initialization lists ?

Daniel919 Daniel919 at web.de
Wed Jul 4 16:53:49 PDT 2007


Hi, in a c++ faq, it's said that they are more efficient than using 
assignment in ctor, because they avoid the need to create/delete a 
temporary object, that contains the actual value.

So, according to the article 
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6,
this is prefered...

   class Foo {
     int x;
     Foo(int i) : x(i) {};
   };

...over

   class Foo {
     int x;
     Foo(int i) { x = i; };
   };


In the former case, when the ctor "creates" the class, it directly
assigns the value of i to x, as if someone had declared it like:
   class Foo {
     int x = i; //Impossible of course
     Foo(int i) : x(i) {};
   };

In the latter case "i" is the temporary object, that only
exists to get its value assigned to x.


How does D handle this ? There are no initialization lists.

But I wonder, are they necessary at all ?
Couldn't the optimizer just look over the complete ctor, recognize this 
simple assignments and automatically assign the values, the way it's 
done with initialization lists in c++ and so avoid the creation of 
temporary objects ?


Daniel



More information about the Digitalmars-d mailing list