tags:

views:

386

answers:

5

Hi,

As far as i know, a copy constructor is invoked in the following scenarios :

1) Pass by value
2) Return by value
3) When you create and initialize a new object with an existing object

Here's the program :

#include <iostream> 
using namespace std; 
class Example 
{ 
    public:
        Example() 
        {
            cout << "Default constructor called.\n";
        }
        Example(const Example &ob1) 
        {
            cout << "Copy constructor called.\n";
        }
        Example& operator=(const Example &ob1) 
        {
            cout << "Assignment operator called.\n"; 
            return *this;
        }
        ~Example()
        {
            cout<<"\nDtor invoked"<<endl;
        }
        int aa;
};

Example funct() 
{
    Example ob2;
    ob2.aa=100;
    return ob2;
}



int main() 
{
    Example x;
    cout << "Calling funct..\n";
    x = funct();
    return 0;
}

The output is:

Default constructor called.

Calling funct..

Default constructor called.

Assignment operator called.

Dtor invoked

Dtor invoked

Please correct me, IIRC the following sequence of calls should occur :

1) Constructor of x is called

2) Constructor of ob2 is called

3) The function returns and so copy constructor is invoked (to copy ob2 to unnamed temporary variable i.e funct() )

4) Destructor of ob2 called

5) Assign the unnamed temporary variable to x

6) Destroy temporary variable i.e invoke its destructor

7) Destroy x i.e invoke x's destructor

But then why copy constructor is not invoked and also only 2 calls to dtors are there whereas i expect 3.

I know compiler can do optimizations, however, is my understanding correct ?

Thanks a lot :)

Regards

lali

+14  A: 

A copy constructor might not be invoked when you return by value. Some compilers use return value optimization feature.

Read about "Return Value Optimization"

Prasoon Saurav
agree, test in debug with no optimizations or create a more slightly complicated test case.
slurdge
The same behavior is manifested even if optimization are not turned on.
Alexandru
GCC has a special command (-fno-elide-constructors) to turn off this optimization which is otherwise always on.
UncleBens
Compilers often do this even with optimizations disabled, because it is explicitly allowed by the standard.
jalf
@UncleBens: thanks a tonne for that information. When compiled with -fno-elide-constructors, everything works as expected. I got the point. Thanks :)
ghayalcoder
This is called "copy elision" of which "RVO" is a subset, I think its worth referring it to its proper name. (Especially since copy elision happens for contexts other than return statements).
Richard Corden
+2  A: 

When doing x = funct(); the compiler notices that it will be directly returned and thus avoids a useless construction. That's also why you will only get two destructor calls.

This is a example why sometimes working with "copy" isn't necessarily a lost of performances.

Tristram Gräbener
"a useless construction". The point here though is that the standard specifically permits this particular optimization even though the constructor has side effects, and hence isn't really "useless". The compiler cannot in general elide code that writes output to `cout`, because the output of a C++ program is considered significant, even if the whole program is "useless" in the everyday sense ;-)
Steve Jessop
A: 

In your exampe the structure is small enough therefore it is passed through a register. The generated code is similar to Return value optimization. Construct a more complicated example, and you'll see the behavior expected.

Alexandru
+4  A: 

The part of the standard which tells you when compilers may elide copies is 12.8/15. It's always up to the compiler whether to do actually perform the elision. There are two legal situations, plus any combination of them:

  • "in a return statement in a function with a class return type, when the expression is the name of a non-volatile automatic object with the same cv-unqualified type as the function return type"

  • "when a temporary class object that has not been bound to a reference would be copied to a class object with the same cv-unqualified type".

The former is usually referred to as the "named return value optimization", and it's what permits the output you're seeing in your example. The latter in effect turns copy-initialization into direct initialization, and could occur for instance if your code did Example x = Example();.

Other copy elisions are not permitted, except of course that the usual "as-if" rules apply. So if the copy constructor has tracing in, then the following code must call it:

Example x;
Example y = x;

But if x were otherwise unused, and the cctor had no side-effects, then I think it could be optimized away, just like any other code that does nothing.

Steve Jessop
A: 

g++ v4.4.1 has an option to suppress "elide" optimizations:


tst@u32-karmic$  g++ -fno-elide-constructors Example.cpp -o Example

tst@u32-karmic$  ./Example 

Default constructor called.
Calling funct..
Default constructor called.

Copy constructor called.

Dtor invoked
Assignment operator called.

Dtor invoked

Dtor invoked

As you can see the copy constructor is now called!