Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is copy constructor called only twice here (should be thrice?)

Reply
Thread Tools

Why is copy constructor called only twice here (should be thrice?)

 
 
TechCrazy
Guest
Posts: n/a
 
      07-15-2005
I am compiling like

g++ -O0 MyString.cpp

Should the copy cnstrs be called thrice? Any optimization going on
here?

--------------------------
#include <iostream>
#include <string>
using namespace std;
class MyString: public string {
public:
MyString(const char * str): string(str) {
cout << "copy ctr 1 called" << endl;
}
MyString(MyString const & str): string(str) {
cout << "copy ctr 2 called" << endl;
}
};

MyString getString() {
MyString x("rrr"); //copy ctr 1 called

//This should call copy ctr once to create the temporary
//that will be returned
return x;
}

int main() {
//This should call copy ctr again with the temporary
//created at the end of getString() to create y
MyString y (getString());
}

------------
This program is printing:

copy ctr 1 called
copy ctr 2 called

 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      07-15-2005

"TechCrazy" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> I am compiling like
>
> g++ -O0 MyString.cpp
>
> Should the copy cnstrs be called thrice? Any optimization going on
> here?


The getString() function is eligible for Named Return Value (NRV)
optimization, which eliminates the temporary.

Regards,
Ben


 
Reply With Quote
 
 
 
 
TechCrazy
Guest
Posts: n/a
 
      07-15-2005
But, I turned off all optimization with -O0

 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      07-15-2005
TechCrazy wrote:
> I am compiling like
>
> g++ -O0 MyString.cpp
>
> Should the copy cnstrs be called thrice? Any optimization going on
> here?
>

Yes, the object returned by getString is y.

> --------------------------
> #include <iostream>
> #include <string>
> using namespace std;
> class MyString: public string {
> public:
> MyString(const char * str): string(str) {
> cout << "copy ctr 1 called" << endl;
> }

Not a copy constructor.

Ian
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      07-15-2005
Ian, I think the OP wants to ask why the constructor was called twice than
three times.

ben


 
Reply With Quote
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      07-15-2005
TechCrazy wrote:
>
> But, I turned off all optimization with -O0


Then ask the guys writing your compiler, why NRVO is still
done in that case.

I guess the answer will be something like: NRVO is always on, because
it is an optimization which affects the callee as well as the caller.
If we would allow to disable it, then there could be a situation, where
the caller is compiled for NRVO, while the callee is not. And that will
give you a big crash.

--
Karl Heinz Buchegger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
conversion constructor called twice - why? Alexander Stippler C++ 3 10-31-2005 08:18 AM
Why is the copy ctor called twice here? richardclay09@yahoo.co.uk C++ 10 10-02-2005 06:33 PM
Why is the Base class Constructor getting called twice Robert C++ 6 12-11-2003 12:06 PM



Advertisments