1. Overview
Recently in See C + + Primer5 just see half, summed up c++11 inside did add a lot of new things, if there is no understanding, don't say you write, see others write code estimates will be a bit difficult. C + + Primer5 is a relatively good book to learn c++11. This article summarizes only the new things in the c++11, the old things no longer repeat. All of the code in this article just lists the key code, and all the features have been verified with the compiler, my compilation environment gcc 5.3.1 g++ 5.3.1, it is said that more than 4.7 of the version has supported the majority of C++11 features, vs series of compilers on the c++11 of the support situation is not very understanding, If there is no suitable compiler, you can click here C++shell this is an online C + + compilation system, there are several options to choose C++98,C++11,C++14, etc., you can verify the correctness of c++11 in this area.
2. A long long type
The long long type is not actually present in C + + 98, and is then indexed by the C99 standard, which is implemented on the market most compilers support long long, but this type formally becomes the standard type of C + + in c++11. The standard requires that a long long is at least 64 bits, or 8 bytes. A literal constant uses the ll suffix to denote a long long type, using the ull suffix to denote a unsigned long long.
3. List initialization
C++11 has fully added the ability to list initialization, including Vector,map, value types, structs, and so on, you can use list initialization, and you can return a list of curly braces in a function, before which we can only initialize a list of arrays:
Array list initialize int xx[5]={1,2,3,4,5};int yy[]={6,7,8,9,0};//value type to initialize int a{10}; int B={10};int c={10.123};//Compiler Error, g++ 5.3.1 when the list is initialized for a value type, the compiler will error if there is a loss of precision. List initialization can also be used with struct typedef struct str{ int x; int y;} STR; Str s = {10,20};//list initialization class, must be public member, if containing private member will fail class Cls{public: int x; int y;}; Cls C = {10,20};//vector can not only use list initialization, but can also use lists to assign values, arrays cannot be assigned with a list vector<int>v1={1,2,3,4,5,6,7,8,9};//Initialize vectors <int>v2;v2={3,4,5,6,7}; Assignment//map list initialization map<string,int> m = {{" X", 1}, {" y", 2}, {"Z", 3}};//returns the initialization list with function only shows the key code, the related header file is added// Similarly, the return of a struct, class, or map can also be returned using the initialization list vector<int> Getvector () { return {1,2,3,4,5};} int main () { vector<int> v = getvector (); Cout<<v[0]<<v[1]<<v.size () <<endl; return 0;}
4. NULLPTR NULL pointer
The newly added literal in c++11 represents a null pointer that does not point to any object, and we used to use a predefined macro null to represent null pointers, in fact null values are 0, and the new standard recommends using nullptr instead of NULL
5. CONSTEXPR variables
We typically use const to define constants when we define them, and a constant must be initialized at the time of definition and cannot be changed afterwards. A constant must be initialized with a constant expression, and the value of the constant can be obtained during compilation, but how to determine if an expression is a constant expression, which is usually determined by the programmer himself, for example:
const int a = 20; 20 is a literal value, of course, is a constant expression, so it is no problem to use 20来 to assign a value//However the following code can also be compiled, g++ 5.3.1int a =, const int x = A; int b[x]={0};
Assigning a value to a constant x is a variable a, which should be unreasonable, but the compiler does not report any errors, and of course this error is obvious, but how difficult it is to determine whether an expression is a constant expression in a complex system, for example, a at a glance, we can tell that it is not a constant expression. For this purpose, C++11 provides a new keyword constexpr that uses the constants defined by the keyword to check whether the expression assigned to it by the compiler is a constant expression, such as the code above:
int a = n; constexpr int x =
When the compiler compiles, the error is that a is not a constant. Obviously the CONSTEXPR keyword transfers the check of a constant expression to the compiler, not the programmer itself, so it is safer to use constexpr to define constants than Const.
6. constexpr function
Normal functions are generally not used to assign values to constexpr constants, but c++11 allows you to define a constexpr function, which can be used to calculate results during compilation, so that functions can be assigned to constexpr. Defining the CONSTEXPR function requires adherence to a number of conventions, the return type of the function and the type of all formal parameters should be literal values, in general the function body must have only one return statement.
constexpr int size () {return 42;} constexpr int si = size ();
When initialization is performed, the compiler replaces the call of the function with the result value, and the CONSTEXPR function body can also have statements other than return, but these statements should not perform any operations at run time, such as empty statements, using declarations, and so on. The CONSTEXPR function allows its return value to be not a literal, for example:
constexpr int size (int s) {return s*4;} int a = 20;const int b = 30;constexpr int c = 40;constexpr int si = size (a); Error A is a variable so the function returns a variable value constexpr int si1 = size (20); The OK function returned is actually a constant constexpr int si2 = size (b); okconstexpr int si3 = size (c); Ok
The CONSTEXPR function does not necessarily return a constant, and if the argument applied to the function is a constant expression returns a constant, otherwise the variable is returned, and the function call is either a constant expression or a const expression that is judged by the compiler. This is the benefit of constexpr.
7. Using Type aliases
Type aliases in fact, in the C language, in general, we use the keyword typedef to declare a type of alias, in C++11 added another way to declare the type alias is to use the Using keyword, the Using keyword is generally used to reference the namespace before c++11.
typedef int INT; The right symbol represents the left using INT2 = int; The left symbol represents the right int a = 20;int2 B = 30;
8. Auto Type Indicator
When we define a variable, we must first determine the type of the variable, and many times not we need a variable and then assign the appropriate data to the variable, but we have a value but we do not know what type of variable to store it, especially the C + + template used very widely, Sometimes you define a variable whose type is a complex type parameter with a template, such as one of the most common examples:
Map<string,int> m; Map<string,int>::iterator it = M.begin ();
In the example above we have defined a variable of type Map<string,int>::iterator to hold the value of M.begin (), which is relatively not difficult but I have been dizzy when I started using the map container, if map is a The map<string,double> type needs to define a map<string.double>::iterator it to store. Especially if the map and vector are nested between each other, it is more likely to be mistaken. Another drawback to defining this type of variable is that a type's name tends to be long, just imagine that this variable is declared throughout the program code, and I'm afraid there won't be a few people looking comfortable. But it's okay. C++11 defines a new keyword auto for defining variables, and the type of the variable is deduced automatically by the compiler based on the assignment expression, and does not require us to display the definition. Because the type of the variable defined by auto is deduced by the compiler based on an assignment, the variable defined by auto must have an initial value, or the compiler cannot determine the type of the variable.
Auto x = 20; X is Intauto y = 3.14; Y is doublemap<string,int> m; Auto it = M.begin (); It is Map<string,int>::iterator
This is not a lot of convenience, and the program looks more concise
Auto can declare multiple variables in one statement, but make sure that there is only one underlying data type in the statement, for example:
Auto i=10,*p=&i; OK i is int,p is Int*auto a=10,b=3.14; is the Error type int or double?
9. Decltype Type Indicator
Sometimes there is the need to know the type of an expression and use that type to define a variable, for example:
int a = 10;int b = 20;auto C = a + B; The type of OK a+b is int, at this point the type of C is int, and the value of C is a+b
Auto solves some of the problems, such as the type of the variable we define is the type of the expression a+b, but if we just need to define a variable of the same type as the expression a+b, but we don't want to assign the value of the expression A+b to the variable we just defined, We want to assign another value or simply define the variable without assigning a value. This would require the use of another type descriptor provided by C++11 Decltype. Decltype acts on an expression and returns the type of the expression, in which the compiler parses the type of the expression and does not evaluate the value of the expression. For example
int a = 10;int B = 20;decltype (a+b) c = 50; The type of OK C is the type of a+b int
For reference types Decltype There are some special places:
int a = &b; int = A;decltype (b) C; Error c is a reference type that must be assigned a value of Decltype (b) d = A; OK D is a reference type, pointing to a
You can see that decltype if it works on a reference type, it gets a reference type. We know that a reference type is usually treated as a synonym for its associated variable when used, for example, if you use cout<<b<<endl; where B is actually equivalent to a, but Decltype preserves the referential nature when it acts on the reference type.
If an expression is an operation to dereference a pointer, Decltype gets a reference type as well:
int a = *p, int = &a;decltype (*p) c = A; The type of c is int&c = 50;cout<<a<<endl; Output 50
When Decltype acts on a variable, the variable is distinguished by the addition of parentheses, for example:
int a = 20;decltype (a) b = 30; The type of OK B is intdecltype ((a)) C = A; The type of OK C is int& its associated variable a
With parentheses, the compiler treats (a) as an expression, and the variable is an expression that can be used as the lvalue of an assignment statement, so it is interpreted as a reference type.
Keep your eye on the next series C++11 new features (2)
If you think this article is helpful to you, need your "like", so that more people can see OH
C++11 new Features Summary (i)