initializer lists and uniform initialization
play

Initializer lists and uniform initialization slides based on talk - PowerPoint PPT Presentation

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 :


  1. Initializer lists Uniform initialization Initializer lists and uniform initialization – slides based on talk by Bjarne Stroustrup Jon Elverkilde April 25, 2008

  2. Initializer lists Uniform initialization Initializer lists • myClass : myInt(1), myContainer() ...

  3. Initializer lists Uniform initialization Initializer lists • myClass : myInt(1), myContainer() ... • int my array[] = { 1,2,3 } ;

  4. 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);

  5. 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;

  6. 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);

  7. 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);

  8. 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);

  9. 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”.

  10. 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 } );

  11. Initializer lists Uniform initialization How? • provide basic type initializer list < T > • classes can provide initializer list-constructor

  12. 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)

  13. 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.

  14. 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 } .

  15. 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