initialization in initializer list using copy constuctor?!?

Discussion in 'C++' started by Alexander Stippler, Apr 9, 2004.

  1. 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
     
    Alexander Stippler, Apr 9, 2004
    #1
    1. Advertising

  2. Alexander Stippler

    Leor Zolman Guest

    On Fri, 09 Apr 2004 14:42:48 +0200, Alexander Stippler
    <-ulm.de> 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
     
    Leor Zolman, Apr 9, 2004
    #2
    1. Advertising

  3. >>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?
     
    Alexander Stippler, Apr 9, 2004
    #3
  4. Alexander Stippler

    Leor Zolman Guest

    On Fri, 09 Apr 2004 15:49:55 +0200, Alexander Stippler
    <-ulm.de> 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
     
    Leor Zolman, Apr 9, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tapeesh

    Default constuctor

    Tapeesh, Mar 1, 2005, in forum: C++
    Replies:
    4
    Views:
    476
    John Carson
    Mar 2, 2005
  2. Replies:
    18
    Views:
    527
    Steve Pope
    Oct 5, 2006
  3. Sid K
    Replies:
    2
    Views:
    288
    Sid K
    Aug 7, 2008
  4. , India
    Replies:
    3
    Views:
    400
    prashanta
    Dec 30, 2008
  5. Unknown Poster

    still crabby about copy constuctor craziness

    Unknown Poster, Jul 24, 2004, in forum: Perl Misc
    Replies:
    16
    Views:
    211
    Ilya Zakharevich
    Aug 2, 2004
Loading...

Share This Page