C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Optimizing away copy constructor

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
ejo
Guest





PostPosted: Mon Sep 12, 2005 9:28 am    Post subject: Optimizing away copy constructor Reply with 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?


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





PostPosted: Mon Sep 12, 2005 2:48 pm    Post subject: Re: Optimizing away copy constructor Reply with quote




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





PostPosted: Mon Sep 12, 2005 2:50 pm    Post subject: Re: Optimizing away copy constructor Reply with quote



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





PostPosted: Mon Sep 12, 2005 2:51 pm    Post subject: Re: Optimizing away copy constructor Reply with quote

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





PostPosted: Tue Sep 13, 2005 12:30 am    Post subject: Re: Optimizing away copy constructor Reply with quote

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





PostPosted: Tue Sep 13, 2005 9:09 am    Post subject: Re: Optimizing away copy constructor Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.