C ++ primer Reading Notes c ++ 11 (1) and primer Reading Notes 11
1. New Keyword nullptr
C ++ 11 introduces a new keyword nullptr, which is used to represent a NULL pointer and is used to replace the NULL provided by c (the original NULL is the macro definition defined in stdlib, usually 0 ).
2. New alias definition mechanism alias declaration
C ++ provides the alias mechanism defined by typedef, but sometimes it is hard to understand. To better use the alias mechanism, c ++ 11 provides a new alias definition mechanism. Similar code is as follows:
// alias declarations using VInt = int; // type alias using PInt = int *; // pointer alias using RInt = int &; // reference alias using MyFunc = int (*)(void); // function pointer alias VInt i = 99; PInt pi = &i; RInt ri = i; MyFunc func = nullptr;
3. auto
C ++ 11 extends the auto function. The compiler can deduce the required parameter types based on the actual initialization parameters and initialize them. Its usage is similar to the following code:
// The result of item Initialization is val1 + val2: auto item = val1 + val2;
Note that when auto, const, and reference types are used together, the syntax may not be a simple type inference, the compiler's inference type mechanism may be inconsistent with the simple inference we can think. That is to say, if you need to define auto references and auto constants, You need to specify them explicitly. Otherwise, directly using the auto compiler will simply use the corresponding types, and directly ignore the const and reference restrictions. The simple sample code is as follows:
/* auto specifier */ // a_i is int, a_pi is a pointer to int, a_ri is a reference to int auto a_i = 0, *a_pi = &a_i, &a_ri = a_i; // error like this, for a_f is float while a_d is double //auto a_f = 0.0f, a_d = 0.0;
4. decltype, the keyword for automatic type inference
C ++ 11 provides the new type inference keyword decltype, which is mainly used to automatically deduce the expression type, but does not require initialization (auto provides the inference type + initialization ).
// result is any type that expr has.decltype(expr) result;
Compared with auto type inference, decltype's automatic type inference is very direct. What type is expr, and usually decltype returns the same type. The usage is as follows:
/*decltype specifier*/ const int ci = 0, &cr = ci, *cp = &ci; decltype(ci) x = 0;// x has type const int decltype(cr) y = x;// y has type const int & decltype(cr) z;// error,z is a reference and must be initialized decltype(cr+0) b;// b has type int decltype(*cp) c;// error, c is a reference and must be initialized int i = 100; decltype(i) d;// d has type int decltype((i)) e;// error ,e is a reference and must be initialized
Two points need to be distinguished. For situations with parentheses and pointer values, decltype returns the left value reference (the last two errors in the code above ).
5. New for loop syntax, range
The c ++ for loop requires three statements, including initialization, conditions, and increments. for some operations, you need to write a lot of code. You can use range for to traverse each element in the STL container. The syntax is as follows:
for(declaration:expression) statement;
For example, we can write std: string into lowercase letters:
/* range for */ using std::string; string str("UPPER test"); for (auto &c : str) c = tolower(c);
Especially when used with auto and decltype, it can save a lot of code (I believe everyone has the experience of using the STL iterator. The code is very long, but the function is to traverse ).
You must note that range for cannot insert or delete elements to or from the container during use and can only be modified.
6 list initialization mechanism list initialization
In combination with c ++ 11, variable initialization has the following methods:
int x = 0;int x = {0}; // list initializationint x(0);int x{0}; // list initialization
The STL template definition also provides a new situation. The sample code is as follows:
/* vector */ vector<vector<int>> ivec; // list initialization vector<string> strvec{"a", "bc", "def"}; vector<string> v2{"123"}; vector<string> v3{5}; vector<string> v4{5, "s"};
The braces are usually used for list initialization, but some cases are not. For example, the vector constructor actually called by v3 and v4 in the above example (this is the processing done by the compiler, when the types in the initialization list do not match the template types ).
Additional instructions
This article is mainly about how c ++ primer covers c ++ 11 in the first four chapters of the Fifth Edition.
All the code has been compiled and tested in gcc v4.8.1. The source code can be downloaded from my git. The url is as follows.