Declaration and Initializations
Universal Initialization
Section titled “Universal Initialization”- Brace Initialization: Can be used with any type.
// do thisvector <int> vec{4,2};// instead of thisvector <int> vec;vec.push_back(4);vec.push_back(2);
Advantages
Section titled “Advantages”- Narrowing conventions are not allowed.
int x = 7.7; // legal, only compile warningint x{7.7}; // illegal, won't compile
- It is consistent.
vector <int> old_one(4) // 0,0,0,0vector <int> old_one(4,2) // 2,2,2,2vector <int> uni{4} // 4vector <int> uni {4,2} // 4,2
- Avoids ambiguity.
Test test(); // object creation or function declaration?Test test{}; // object creation!
nullptr
Section titled “nullptr”- Literal representing a null pointer.
- It has a special type which is compatible with any pointer type, but cannot be converted to an integer.
void func(int);void func(int *);func(nullptr); // calls func(int *) as expected
- The traditional NULL has the value of 0.
- It’s type is implementation defined.
func(NULL); // Clang: calls func(int *), VC++: calls func(int), GCC: Won't compile