C++11 new Features

Source: Internet
Author: User

C++11 new features are very much, a simple feature if you want to explain clearly and attach the case may need to write a lot (people feel that the article is too long to understand), this film is only about some of the features, the opportunity to do in-depth interpretation of individual features. The following are just some of the parts that people often use during their use, and now do the following explanations and mistakes .

1. Right value and move semantics

C++11 adds a new very-number reference (reference) type, called an rvalue reference (R-value Reference), labeled T &&. The temporary object referenced by an rvalue reference can be modified after the temporary object is initialized, in order to allow the move semantics, as described in "original" C + + 11 Rvalue reference

Note: The container in the STL supports move semantics, so efficiency is not a problem when returning local variables

2. Modification of pod definition (plain old data)

a minimalist type or structure conforms to the following definition: Minimalist default constructor. This can use the default constructor syntax, such as someconstructor () = default;

(1). Minimalist copy-constructor, default syntax is used

(2). The minimalist assignment operator, which can be used with the default syntax (syntax)

(3). Minimalist deconstruction, can not be fictitious (virtual)

The type or structure of a standard layout (standard-layout) conforms to the following definitions:

(1). Have the same access control for all Non-static members (public, private, protected)

(2). No virtual function

(3). No virtual base class

(4). Only base classes that conform to the standard layout

(5). There is no base class of the same type as the first defined non-static member

(6). If there is no base class with non-static members, then the lowest (least inherited) type has no non-static data member and at most one base class with non-static members. Basically, there will only be one type with non-static members in the inheritance system of that type.

(7). Only non-static (non-static) data members, and these members are also types that conform to the standard layout?

3. Initialization list

until the introduction of C + + 11, only arrays can use the initialization list. In C + + 11, it's all legal:

1 int a{5};?

2 char c{' X '};

3 int P[5] = {1, 2, 3, 4, 5};

4 Vector vcttemp{1, 2, 3};

5 CPerson person{10, "Mike"};

6 int b = 5.3; B is assigned to 5, and a narrow conversion occurs.

7 int d{5.3}; will prompt for compilation errors and avoid narrowing conversions

Map M_map = {"Test", 1},{"Test2", 2}};? No need to construct key-value pairs

Vector Test ()

{       

return{"1", "2", "3"};// list initialization return value

}

4, type deduction ??

A variable that has been explicitly initialized can use the Auto keyword, and decltype can be used to determine the type of an expression at compile time

Auto Somestrangecallabletype = Boost::bind (&somefunction, _2, _1, someobject);?

The type of Somestrangecallabletype is the type that the template function Boost::bind returns to a particular argument. As part of the compiler's semantic analysis responsibility, this type can be easily determined by the compiler, but it is not so easy for the user to judge the type by looking at it.

int someint;

Decltype (someint) otherintegervariable = 5;//variable type int

Can the type represented by Decltype be different from auto derivation?

Const Std::vector V (1); Auto A = v[0];//A is of type int

Decltype (v[0]) b; b for const int& type, i.e. std::vector::operator[] (size_type) const return type

Auto C = 0; C for INT type

Auto d = c; D for INT type

Decltype (c) E; e is int type, c actual type

Decltype ((c)) F = e; F is int& type, because (c) is an lvalue

Decltype (0) G; G is int type, because 0 is the right value?

5. Lambda function and expression

When using C + + standard library algorithm functions such as sort and find, users often want to be able to define a temporary section function (aka function, predicate functions) near the algorithm function call.

??????? Lambda expressions in C + + 11 are used to define and create anonymous function objects to simplify the programming effort. The syntax for Lambda is as follows:

[function Object Parameters] (operator overloaded function parameter) mutable or exception declaration, return value type {function Body}. As you can see, lambda is mainly divided into five parts: [function Object parameter], (operator overloaded function parameter), mutable or exception declaration, return value type, {function body}. The following are described separately.

(1), [function object parameter], identifies the beginning of a lambda, this part must exist, cannot be omitted. A function object parameter is a constructor that is passed to the compiler's automatically generated function object class. The function object parameter can only use local variables that are visible to the scope of the lambda in which it is defined (including the this of the class that the lambda resides in). Function object parameters have the following form:

1, empty. No function object parameters are used.

2, =. The body of the function can use all the visible local variables within the scope of the lambda's scope (including this of the class that the lambda is in), and it is the way the value is passed (equivalent to the compiler automatically passing all local variables for us by value).

3, &. The body of the function can use all the visible local variables within the scope of the lambda's scope (including this of the class that the lambda is in), and it is a reference delivery method (equivalent to the compiler automatically passing all local variables for us by reference).

4, this. The body of a function can use member variables in the same class as lambda.

5, A. The A is passed by value. When passing by value, the body of a function cannot modify the copy of a passed in because the function is const by default.       To modify a copy of a passed in, you can add the mutable modifier. 6, &a. The A is passed by reference.

7, A, &b. The A is passed by value and B is passed by reference.

8, =,&a, &b. In addition to a and B are passed by reference, other parameters are passed by value.

9, &, A, B. Except for A and B are passed by value, other parameters are passed by reference.

(2), (operator overloaded function parameter), identifies the parameters of the overloaded () operator, which can be omitted when there are no parameters. Parameters can be passed by value (such as: (A, b)) and by reference (for example: (&A,&B)).

(3), mutable or exception declaration, this part can be omitted. When you pass a function object parameter by value, you can modify the copy passed in by value (Note that you can modify the copy rather than the value itself) when you add the mutable modifier. The exception declaration is used to specify the exception that the function throws, such as an exception that throws an integer type, and you can use throw (int).

(4), the return value type, which identifies the type of the function return value, which can be omitted when the return value is void, or where there is only one return in the function body (at which point the compiler can automatically infer the return value type).

(5), {function Body}, identifies the implementation of the function, this part cannot be omitted, but the function body can be empty.

when using QT, the slot function of connect supports lambda expressions

For_each (Vcttemp.begin (), Vcttemp.end (), [&] (int v) mutable{cout << v+a << Endl; a++;});? To pass all visible local variables within the scope, including this, in a reference way

6. Strongly typed enumeration

in standard C + +, enumeration types are not type-safe, and the only security mechanism provided by C++03 is that an integer or an enumerated value cannot be implicitly converted to another enum type.

Enum class TYPEA{A1 = 1, a2};

A1 = = 1;? Statement will error

The names of the enumerations are all exposed to the general scope, so two different enumerations can not have the same enumeration name?

Enum A{a, B};enum B{a, A is illegal in the c};//American drama?

7. Explicit type conversion sub (explicite) to prevent implicit conversion of parameters

8, Static assertion
????? Static_assert (3.14< greekpi && greekpi< 3.15, "GREEKPI is inaccurate!"); Parameter 1 if False, the string that pops up the parameter 2

9, constexpr: constexpr can be said to be the extension of the const variable, it is just the right side of the constraint const variable must be able to compile the value of the calculation

10, type alias?

typedef int (addptr*) (int a,intb);?

This can be done in c++11: using addptr = Int (int,int);

11, nullptr

Nullptr is a newly added keyword in c++11 that identifies a null pointer. After introducing nullptr, we can solve the two semantic problems of some function overloads.

If there are methods void f (int) and Method VOIDF (char *p), then call F? (0) When compiling fails in c++98, there are two semantic compilers that are good, but in c++11, call F (int), Method F (char*) call mode: F (nullptr).

12. Default or disable

When we define our own constructor with parameters, the compiler will no longer generate a default constructor, and if you want to use the default constructor at this point, you must explicitly declare and define the constructor without parameters. In c++11, we can use the default keyword to indicate that we want to use the defaults constructor. Similarly, when we do not want to externally use constructors or assignment functions that are automatically generated by the compiler, we generally need to declare them as protected or private. In c++11, we can use the DELETE keyword to indicate that we do not want the compiler to generate a default constructor or assignment function

CPerson () = default; The CPerson constructor

CPerson (const CPerson &person) =delete;? Copy constructor for CPerson

13, template right double bracket

In C++98, vector>vcttemp is an illegal expression, and there must be a space in the middle of the two right angle brackets in the template, but the expression in c++11 is legal

Cite articles

c++11 (new C + + standard)??

C++11 New Standard

C++11 new Features

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.