Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unexpected copy constructor

Reply
Thread Tools

unexpected copy constructor

 
 
ciccio
Guest
Posts: n/a
 
      05-20-2010
Hi,

I recently stumbled upon something rather annoying. When doing a quick
return in a function, it is possible that a copy constructor is called.
This is rather surprising.

The code below demonstrates this and outputs the word "copy".
Is this a bug in the compiler or something from C++? Such a copy
constructors are often really not wanted.

Thanks for the help.

This was compiled with g++ version 4.4.3.

#include <iostream>

class foo {
public:
foo() {}
foo(const foo&f) {std::cout << "copy" << std::endl;}
};

foo function(int i) {
if (i > 0) return foo();
foo f;
return f;
}

int main(void) {
function(-1);
}
 
Reply With Quote
 
 
 
 
Jonathan Lee
Guest
Posts: n/a
 
      05-20-2010
On May 20, 10:37*am, ciccio <(E-Mail Removed)> wrote:
> I recently stumbled upon something rather annoying. When doing a quick
> return in a function, it is possible that a copy constructor is called.
> This is rather surprising.


Actually, it's the normal behavior. How else is your "f" object
supposed to get from the function scope to the containing scope?
At least conceptually, it supposed to be destroyed at the end of
its own scope. So a copy is made and the original destroyed.

> The code below demonstrates this and outputs the word "copy".
> Is this a bug in the compiler or something from C++? Such a copy
> constructors are often really not wanted.


You're right, they're not. C++ compilers are allowed to elide
the copy, i.e., skip it. I guess it depends on the compiler, but
basically they construct the value to be returned in the place
where it is going to be returned to. Thus eliminating the copy
(and destruction).

You'll probably want to look into return value optimization
(or just RVO). Also, check out the FAQ at parashift.com

Whether or not RVO is performed seems to depend a lot on your
compiler. One thing that can sometimes throw it off is if
you are not returning the same object at all points in your
function. Like, if every return statement returned "f" then
you'd probably be okay. The fact that one place returns "f"
and the other "foo()" might cause a problem.

You gotta sorta test things yourself, or find your compiler's
documentation that says specifically what triggers or blocks
RVO.

--Jonathan
 
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
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Calling base class constructor from derived class Copy constructor ali C++ 4 03-05-2007 09:15 AM
deep/shallow copy - constructor v Object.copy() VisionSet Java 8 04-29-2004 10:41 PM
Copy constructor hides default constructor Aire C++ 3 01-25-2004 05:47 PM



Advertisments