More instantiation and ctor call

  • Thread starter Robert Sturzenegger
  • Start date
R

Robert Sturzenegger

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
 
J

John Harrison

Robert Sturzenegger said:
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
 
J

John Harrison

John Harrison said:
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
 
J

Josephine Schafer

Robert Sturzenegger said:
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.
 
G

Gavin Deane

John Harrison said:
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
 
K

Karl Heinz Buchegger

Robert said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top