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 

Calls to Copy Constructors

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





PostPosted: Mon Oct 24, 2005 9:06 am    Post subject: Calls to Copy Constructors Reply with quote



Hi all,
I have confusion regarding exact number of called to a CC, since I have
seen compilers "optimizing them". Following program illustrates my
point:

class X{ };

X f()
{
X ret;
return ret;
}

int main()
{
X val = f() ;
}

So the questions are :

1) what is the exact sequence of constructor/CC/dtor in this code?
I state what I believe ie correct -
a) main calls f(). In f() ret is constructed using ctor call.
b) ret is copied to a temporary using a CC call.
c) ret is destroyed as the control reaches end of main.
d) the temporary is copied to val using call to CC of X
e) The temporary is destroyed
f) Finally when control reaches end of main, val is destroyed.

Is this correct?

2) If f() would have been written as

X f()
{
return X(); // Return value optimization
}

then would any temporary be involved? (i.e. would the result of X() be
built into the temporary or directly into 'val'? ) Would the process
involve any call to CC?

3) Is it legal for compilers to "optimize away" these CC calls, if they
can?


Thanks,
Neelesh


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Francis Glassborow
Guest





PostPosted: Mon Oct 24, 2005 4:42 pm    Post subject: Re: Calls to Copy Constructors Reply with quote



In article <1130131039.523443.172820 (AT) z14g2000cwz (DOT) googlegroups.com>,
Neelesh <neelesh.bodas (AT) gmail (DOT) com> writes
Quote:
3) Is it legal for compilers to "optimize away" these CC calls, if they
can?

The C++ Standard explicitly authorises optimising away copy constructors

in many circumstances even if the behaviour of the resulting code is
different. It also authorises a compiler to utilise an extra copy ctor
in many cases.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ 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.