Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialization in initializer list using copy constuctor?!?

Reply
Thread Tools

initialization in initializer list using copy constuctor?!?

 
 
Alexander Stippler
Guest
Posts: n/a
 
      04-09-2004
Hi,

I wonder about the behaviour of como and icc on some very simple program. I
thought initializing members of classes, which are of class type, would be
'direct initialized' (as the standard says). But in the example below the
copy
constructor of Vec is executed when initializing the Ref object. Is this
standard conform? Doesn't it result in bad performance?

#include <iostream>

using namespace std;

class Vec
{
public:
Vec() { cout << "constructor" << endl;}
Vec(const Vec &v) { cout << "copy constructor" << endl; }
};

class Ref
{
public:
Ref() : vec_(Vec()) { }

private:
Vec vec_;
};

int
main()
{
Ref ref;

return 0;
}


regards,
alex
 
Reply With Quote
 
 
 
 
Leor Zolman
Guest
Posts: n/a
 
      04-09-2004
On Fri, 09 Apr 2004 14:42:48 +0200, Alexander Stippler
<> wrote:

>Hi,
>
>I wonder about the behaviour of como and icc on some very simple program. I
>thought initializing members of classes, which are of class type, would be
>'direct initialized' (as the standard says). But in the example below the
>copy
>constructor of Vec is executed when initializing the Ref object. Is this
>standard conform?


You're perhaps confusing "direct initialization" with "default
initialization". A line of code that invokes either of your two
constructors "directly" is "direct initialization. For example,

Vec v; // default initialization
Vec v(v2); // direct initialization
Vec v = v2; // not direct initialization, but still initialization
v = v2; // not initialization at all

> Doesn't it result in bad performance?

Not at all; you have to do what you have to do!

>
>#include <iostream>
>
>using namespace std;
>
>class Vec
>{
> public:
> Vec() { cout << "constructor" << endl;}
> Vec(const Vec &v) { cout << "copy constructor" << endl; }
>};
>
>class Ref
>{
> public:
> Ref() : vec_(Vec()) { }


If you had omitted the base initializer totally, or used it but omitted the
"Vec()" from inside the parens, it would have called the default
constructor.
-leor

>
> private:
> Vec vec_;
>};
>
>int
>main()
>{
> Ref ref;
>
> return 0;
>}
>
>
>regards,
> alex


--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
 
Alexander Stippler
Guest
Posts: n/a
 
      04-09-2004
>>class Ref
>>{
>> public:
>> Ref() : vec_(Vec()) { }

>
> If you had omitted the base initializer totally, or used it but omitted
> the "Vec()" from inside the parens, it would have called the default
> constructor.
> -leor


Consider Vec() replaced by e.g Vec(a,b,c). Why is it necessary to (1) create
Vec(a,b,c) and then (2) copy it into vec_ using the copy constructor?
Shouldn't it just directly initialize the vec_ object?
By the way, gcc does exactly this, como and icc not. This situation is IMO
comparable to copy constructors given a temporary class object, e.g.

Vec v(Vec());

would result in the direct initialization of v with the temporary Vec(), not
first create it and then copy it into v. Where is the difference?
 
Reply With Quote
 
Leor Zolman
Guest
Posts: n/a
 
      04-09-2004
On Fri, 09 Apr 2004 15:49:55 +0200, Alexander Stippler
<> wrote:

>>>class Ref
>>>{
>>> public:
>>> Ref() : vec_(Vec()) { }

>>
>> If you had omitted the base initializer totally, or used it but omitted
>> the "Vec()" from inside the parens, it would have called the default
>> constructor.
>> -leor


First of all, sorry, I said "base initializer" when of course it is a
"member" initializer. No idea why my brain did that (not enough coffee yet,
I guess....)

>
>Consider Vec() replaced by e.g Vec(a,b,c). Why is it necessary to (1) create
>Vec(a,b,c) and then (2) copy it into vec_ using the copy constructor?


Okay, several issues here. First, are you aware that you do not need, in
/either/ case, to use the "temporary" syntax? IOW, you can either omit the
member initializer completely (which invokes vec_'s default constructor)
or, to pass the ctor args, use:
Ref() : vec_(a,b,c) {}

This results in the best performance imaginable. The fact that some
compilers take your version and optimize away the temporaries, but others
don't, is a QOI issue...and I wouldn't even say the ones that do not suffer
from lower QOI, but rather than their implementors chose to focus on other
features (perhaps "export" in the case of EDG, the authors of the Comeau
front-end) that were likely to make more of a difference to more folks.

>Shouldn't it just directly initialize the vec_ object?
>By the way, gcc does exactly this, como and icc not. This situation is IMO
>comparable to copy constructors given a temporary class object, e.g.
>
>Vec v(Vec());
>
>would result in the direct initialization of v with the temporary Vec(), not
>first create it and then copy it into v. Where is the difference?


QOI.
-leor

--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: Download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
value & default initialization, and copy initialization Taras_96 C++ 3 10-30-2009 10:51 AM
copy initialization and direct initialization from C++ Primer pauldepstein@att.net C++ 5 03-26-2009 06:32 PM
Is this copy initialization or direct initialization ? subramanian100in@yahoo.com, India C++ 3 12-30-2008 12:57 PM
initialization of array as a member using the initialization list aaragon C++ 2 11-02-2008 04:57 PM
initializer list of copy constructor ccs C++ 2 06-07-2004 01:06 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57