Initializer lists Uniform initialization Initializer lists and uniform initialization – slides based on talk by Bjarne Stroustrup Jon Elverkilde April 25, 2008
Initializer lists Uniform initialization Initializer lists • myClass : myInt(1), myContainer() ...
Initializer lists Uniform initialization Initializer lists • myClass : myInt(1), myContainer() ... • int my array[] = { 1,2,3 } ;
Initializer lists Uniform initialization C and C++ style initialization C style: • X a = { v } ; • X a = v; C++ additions: • new X(v); • X a(v); • X(v);
Initializer lists Uniform initialization What’s the problem? Consider the task of initializing a vector with the values 1,2,3: 1. std::vector < int > v;
Initializer lists Uniform initialization What’s the problem? Consider the task of initializing a vector with the values 1,2,3: 1. std::vector < int > v; 2. v.push back(1);
Initializer lists Uniform initialization What’s the problem? Consider the task of initializing a vector with the values 1,2,3: 1. std::vector < int > v; 2. v.push back(1); 3. v.push back(2);
Initializer lists Uniform initialization What’s the problem? Consider the task of initializing a vector with the values 1,2,3: 1. std::vector < int > v; 2. v.push back(1); 3. v.push back(2); 4. v.push back(3);
Initializer lists Uniform initialization What’s the problem? Now consider the array: • int my array[] = { 1,2,3 } ; • That’s not fair! • and it violates a fundamental design principle: “provide as good support for user-defined types as for built-in types”.
Initializer lists Uniform initialization What’s the problem? Now consider the array: • int my array[] = { 1,2,3 } ; • That’s not fair! • and it violates a fundamental design principle: “provide as good support for user-defined types as for built-in types”. • So this is what we get: • vector < int > v = { 1,2,3 } ; • template < class T > sum(const vector < T > &); • int x = sum( { 1,2,3 } );
Initializer lists Uniform initialization How? • provide basic type initializer list < T > • classes can provide initializer list-constructor
Initializer lists Uniform initialization How? • provide basic type initializer list < T > • classes can provide initializer list-constructor When {} is used: 1. Check for initializer list-constructor 2. Check for normal constructor 3. Check C-style (array, struct)
Initializer lists Uniform initialization Uniform initialization Different syntaxes mean different things: • X t1 = v; • X t2(v); • X t3 = { v } ; • X t4 = X(v); X and v can be chosen so that 0,1,2,3 or 4 of these compile.
Initializer lists Uniform initialization Solution With initializer lists: • X x1 = X { 1,2 } ; • X x2 = { 1,2 } ; • X x3 { 1,2 } ; • X* p2 = new X { 1,2 } ; These all mean initialize X with initializer list { 1,2 } .
Initializer lists Uniform initialization Sources This presentation was based on a (1 hour) Google Talk by Bjarne Stroustrup: http://video.google.com/videoplay?docid= 5262479012306588324 or find at Stroustrup’s site (bottom): http://www.research.att.com/~bs/C++.html Wikipedia also has a short explanation and some examples: http://en.wikipedia.org/wiki/C%2B%2B0x
Recommend
More recommend