More instantiation and ctor call

Discussion in 'C++' started by Robert Sturzenegger, Jul 29, 2003.

  1. class C
    {
    public:
    C() { cout << "ctor" << endl; }
    C(const C & rhs) { cout << "copy ctor" << endl; }
    ~C() { cout << "dtor" << endl; }
    };

    C c0; // A
    C c1 = C(); // B

    I could imagine, that line B causes the creation of a temporary object which
    is used to intantiate c1 by means of the copy ctor. But it's not the case,
    lines A and B cause the same (tested on several compilers). Is this
    required? Or is the compiler free which way to do that? Is it optimization?

    Thanks!
    Robert Sturzenegger
    Robert Sturzenegger, Jul 29, 2003
    #1
    1. Advertising

  2. "Robert Sturzenegger" <> wrote in message
    news:...
    > class C
    > {
    > public:
    > C() { cout << "ctor" << endl; }
    > C(const C & rhs) { cout << "copy ctor" << endl; }
    > ~C() { cout << "dtor" << endl; }
    > };
    >
    > C c0; // A
    > C c1 = C(); // B
    >
    > I could imagine, that line B causes the creation of a temporary object

    which
    > is used to intantiate c1 by means of the copy ctor. But it's not the case,
    > lines A and B cause the same (tested on several compilers). Is this
    > required? Or is the compiler free which way to do that? Is it

    optimization?
    >
    > Thanks!
    > Robert Sturzenegger
    >


    When you write A a = x; the effect is the same as A a(x); provided x is an
    expression of the same type as A. Otherwise it is a construction of a
    temporary object. This is a requirement, not an optional optimization.

    john
    John Harrison, Jul 29, 2003
    #2
    1. Advertising

  3. "John Harrison" <> wrote in message
    news:bg55iv$k94bm$-berlin.de...
    >
    > "Robert Sturzenegger" <> wrote in message
    > news:...
    > > class C
    > > {
    > > public:
    > > C() { cout << "ctor" << endl; }
    > > C(const C & rhs) { cout << "copy ctor" << endl; }
    > > ~C() { cout << "dtor" << endl; }
    > > };
    > >
    > > C c0; // A
    > > C c1 = C(); // B
    > >
    > > I could imagine, that line B causes the creation of a temporary object

    > which
    > > is used to intantiate c1 by means of the copy ctor. But it's not the

    case,
    > > lines A and B cause the same (tested on several compilers). Is this
    > > required? Or is the compiler free which way to do that? Is it

    > optimization?
    > >
    > > Thanks!
    > > Robert Sturzenegger
    > >

    >
    > When you write A a = x; the effect is the same as A a(x); provided x is an
    > expression of the same type as A. Otherwise it is a construction of a
    > temporary object. This is a requirement, not an optional optimization.
    >
    > john
    >


    What I said is true, but I'm not sure it answers your questions, as it only
    means

    C c1 = C();

    is equivalent to

    C c1(C());

    Not sure what happens from that point on, sorry.

    john
    John Harrison, Jul 29, 2003
    #3
  4. "Robert Sturzenegger" <> wrote in message
    news:...
    > class C
    > {
    > public:
    > C() { cout << "ctor" << endl; }
    > C(const C & rhs) { cout << "copy ctor" << endl; }
    > ~C() { cout << "dtor" << endl; }
    > };
    >
    > C c0; // A
    > C c1 = C(); // B
    >
    > I could imagine, that line B causes the creation of a temporary object

    which
    > is used to intantiate c1 by means of the copy ctor. But it's not the case,
    > lines A and B cause the same (tested on several compilers). Is this
    > required? Or is the compiler free which way to do that? Is it

    optimization?
    >

    Seems like compiler optimisation to me.
    IMO, C c1(C()) may bypass the copy constructor and directly make the
    temporary object c1.

    --
    With best wishes,
    J.Schafer
    Josephine Schafer, Jul 29, 2003
    #4
  5. Robert Sturzenegger

    Gavin Deane Guest

    "John Harrison" <> wrote in message news:<bg55qp$kne72$-berlin.de>...
    > "John Harrison" <> wrote in message
    > news:bg55iv$k94bm$-berlin.de...
    > >
    > > "Robert Sturzenegger" <> wrote in message
    > > news:...
    > > > class C
    > > > {
    > > > public:
    > > > C() { cout << "ctor" << endl; }
    > > > C(const C & rhs) { cout << "copy ctor" << endl; }
    > > > ~C() { cout << "dtor" << endl; }
    > > > };
    > > >
    > > > C c0; // A
    > > > C c1 = C(); // B
    > > >
    > > > I could imagine, that line B causes the creation of a temporary object

    > which
    > > > is used to intantiate c1 by means of the copy ctor. But it's not the

    > case,
    > > > lines A and B cause the same (tested on several compilers). Is this
    > > > required? Or is the compiler free which way to do that? Is it

    > optimization?
    > > >
    > > > Thanks!
    > > > Robert Sturzenegger
    > > >

    > >
    > > When you write A a = x; the effect is the same as A a(x); provided x is an
    > > expression of the same type as A. Otherwise it is a construction of a
    > > temporary object. This is a requirement, not an optional optimization.
    > >
    > > john
    > >

    >
    > What I said is true, but I'm not sure it answers your questions, as it only
    > means
    >
    > C c1 = C();
    >
    > is equivalent to
    >
    > C c1(C());
    >
    > Not sure what happens from that point on, sorry.
    >
    > john


    I believe the compiler is allowed to optimise away the construction of
    the temporary C and default construct c1 directly.

    GJD
    Gavin Deane, Jul 29, 2003
    #5
  6. Robert Sturzenegger wrote:
    >
    > class C
    > {
    > public:
    > C() { cout << "ctor" << endl; }
    > C(const C & rhs) { cout << "copy ctor" << endl; }
    > ~C() { cout << "dtor" << endl; }
    > };
    >
    > C c0; // A
    > C c1 = C(); // B
    >
    > I could imagine, that line B causes the creation of a temporary object which
    > is used to intantiate c1 by means of the copy ctor. But it's not the case,
    > lines A and B cause the same (tested on several compilers). Is this
    > required? Or is the compiler free which way to do that? Is it optimization?
    >


    Your imagination is right: the compiler has to do this and it also has to check,
    if this path is going to work (by eg. checking the accessability of the copy constructor).
    But: The compiler is also allowed to optimize the call of the copy constructor away.
    This is explicitely allowed by the C++ standard, even in the case that the copy
    constructor has side effects as in your case.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Jul 29, 2003
    #6
  7. Thank you for your responses!
    Robert
    Robert Sturzenegger, Jul 29, 2003
    #7
    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. Apricot
    Replies:
    4
    Views:
    511
    velthuijsen
    Apr 16, 2004
  2. NVH
    Replies:
    8
    Views:
    477
    mlimber
    Jul 6, 2006
  3. Grizlyk
    Replies:
    8
    Views:
    463
    Grizlyk
    Nov 29, 2006
  4. , India

    copy ctor vs default ctor

    , India, Aug 15, 2007, in forum: C++
    Replies:
    2
    Views:
    398
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
    Aug 15, 2007
  5. puzzlecracker
    Replies:
    8
    Views:
    416
    James Kanze
    Apr 15, 2008
Loading...

Share This Page