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

initializer lists and uniform initialization
SMART_READER_LITE
LIVE PREVIEW

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 :


slide-1
SLIDE 1

Initializer lists Uniform initialization

Initializer lists and uniform initialization

– slides based on talk by Bjarne Stroustrup

Jon Elverkilde April 25, 2008

slide-2
SLIDE 2

Initializer lists Uniform initialization

Initializer lists

  • myClass : myInt(1), myContainer() ...
slide-3
SLIDE 3

Initializer lists Uniform initialization

Initializer lists

  • myClass : myInt(1), myContainer() ...
  • int my array[] = { 1,2,3 };
slide-4
SLIDE 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);
slide-5
SLIDE 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;
slide-6
SLIDE 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);
slide-7
SLIDE 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);
slide-8
SLIDE 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);
slide-9
SLIDE 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”.

slide-10
SLIDE 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 });
slide-11
SLIDE 11

Initializer lists Uniform initialization

How?

  • provide basic type initializer list<T>
  • classes can provide initializer list-constructor
slide-12
SLIDE 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)
slide-13
SLIDE 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.

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

slide-15
SLIDE 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

  • r 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