| View previous topic :: View next topic |
| Author |
Message |
ejo Guest
|
Posted: Mon Sep 12, 2005 9:28 am Post subject: Optimizing away copy constructor |
|
|
The following code calls the destructor ~B twice:
struct B {
~B() { std::cout << "~B()n"; }
};
struct C: B
{
C(const B &b): B(b) {}
};
C f() { return C(B()); }
int main() { f(); }
Is this behavior enforced by the language or is an implementation
allowed to optimize away the copy constructed B?
Regards,
Erling
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
axter Guest
|
Posted: Mon Sep 12, 2005 2:48 pm Post subject: Re: Optimizing away copy constructor |
|
|
ejo wrote:
| Quote: | The following code calls the destructor ~B twice:
struct B {
~B() { std::cout << "~B()n"; }
};
struct C: B
{
C(const B &b): B(b) {}
};
C f() { return C(B()); }
int main() { f(); }
Is this behavior enforced by the language or is an implementation
allowed to optimize away the copy constructed B?
You can not optimize away the copy constructor. |
You're adding an extra constructed object.
Try the following instead.
C f() { return B(); }
Since you're passing back by value, you will still get extra
constructor call.
You could avoid this by using smart pointers like boost shared_ptr or a
cow_ptr.
This can prevent creation and deletion when returning by value.
Check out the following link for a cow_ptr:
http://code.axter.com/cow_ptr.h
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ismail Pazarbasi Guest
|
Posted: Mon Sep 12, 2005 2:50 pm Post subject: Re: Optimizing away copy constructor |
|
|
When C is constructed, it will have a base B.
When constructor returns, the B() you passed is destroyed. This is the
first ~B you see.
When C destructs, base B's destructor gets called, this is the second
~B.
This is language behavior. The object you pass doesn't become C's base
B (instance), but base B instance is initialized with given B instance.
PS: Use 'virtual ~B()', not bare '~B()'.
Ismail
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Rob Guest
|
Posted: Mon Sep 12, 2005 2:51 pm Post subject: Re: Optimizing away copy constructor |
|
|
ejo wrote:
| Quote: |
The following code calls the destructor ~B twice:
struct B {
~B() { std::cout << "~B()n"; }
};
struct C: B
{
C(const B &b): B(b) {}
};
C f() { return C(B()); }
int main() { f(); }
Is this behavior enforced by the language or is an implementation
allowed to optimize away the copy constructed B?
|
In short, the behaviour is not enforced by the standard. However, in
this case, it is probably not a copy constructed B that is being
optimised out of existance. It is more likely to be a copy constructed
C.
The standard allows an implementation to avoid creating temporary
objects if the only way of detecting that the temporary has been
created is by tracking calls to constructors or destructors.
Optimising out construction of a base class (which, in your example,
happens to be done with B's copy constructor) is not allowed.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
ejo Guest
|
Posted: Tue Sep 13, 2005 12:30 am Post subject: Re: Optimizing away copy constructor |
|
|
axter wrote:
| Quote: | ejo wrote:
The following code calls the destructor ~B twice:
struct B {
~B() { std::cout << "~B()n"; }
};
struct C: B
{
C(const B &b): B(b) {}
};
C f() { return C(B()); }
int main() { f(); }
Is this behavior enforced by the language or is an implementation
allowed to optimize away the copy constructed B?
You can not optimize away the copy constructor.
|
Why? The C constructor is inlined and it should be possible for a
clever implementation to discover that the first B created is just
duplicated to create a base for C, and instead create just one B
directly in the return value C. Would this be in conflict with the
standard?
Regards,
Erling
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ismail Pazarbasi Guest
|
Posted: Tue Sep 13, 2005 9:09 am Post subject: Re: Optimizing away copy constructor |
|
|
The first B is destroyed when constructor returns, it's a temporary
object. There is no B after constructor, so there won't be a base B.
axter's way is fine.
Ismail
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|