C++ Copy constructor, temporaries and copy semantics
Asked Answered
H

3

16

For this program

#include <iostream>
using std::cout;

struct C 
{
    C() { cout << "Default C called!\n"; }
    C(const C &rhs) { cout << "CC called!\n"; }
};

const C f()
{
    cout << "Entered f()!\n";
    return C();
}

int main()
{
    C a = f();
    C b = a;

    return 0;
}

the output I get is:

Entered f()!
Default C called!
CC called!

Since f() is returning by value, it should return a temporary. As T a = x; is T a(x);, wouldn't it call the copy constructor for the construction of a, with the temporary passed-in as its argument?

Hierology answered 24/2, 2010 at 2:15 Comment(0)
C
12

Since f() is returning by value, it should return a temporary. As T a = x; is T a(x);, wouldn't it call the copy constructor for the construction of a, with the temporary passed-in as its argument?

Look up Return Value Optimization. This is turned on by default. If you are on Windows using MSVC 2005+ you can use /Od to turn this off and get the desired result (or -fno-elide-constructors on GCC). Also, for MSVC see this article.

12.8 Copying class objects

15 When certain criteria are met, an implementation is allowed to omit the copy construction of a class object, even if the copy constructor and/or destructor for the object have side effects. In such cases, the implementation treats the source and target of the omitted copy operation as simply two different ways of referring to the same object, and the destruction of that object occurs at the later of the times when the two objects would have been destroyed without the optimization.115 This elision of copy operations is permitted in the following circumstances (which may be combined to eliminate multiple copies):

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, the copy operation can be omitted by constructing the automatic object directly into the function’s return value — in a throw-expression, when the operand is the name of a non-volatile automatic object, the copy operation from the operand to the exception object (15.1) can be omitted by constructing the automatic object directly into the exception object

— when a temporary class object that has not been bound to a reference (12.2) would be copied to a class object with the same cv-unqualified type, the copy operation can be omitted by constructing the temporary object directly into the target of the omitted copy

— when the exception-declaration of an exception handler (Clause 15) declares an object of the same type (except for cv-qualification) as the exception object (15.1), the copy operation can be omitted by treating the exception-declaration as an alias for the exception object if the meaning of the program will be unchanged except for the execution of constructors and destructors for the object declared by the exception-declaration.

Note: Emphasis mine

Covenantor answered 24/2, 2010 at 2:19 Comment(2)
I use GCC, and with -fno-elide-constructors it showed exactly what is happening under the hood! Sometimes these optimizations confuse a learner a lot :) However, I agree that they should be ON by default, for again an uninformed person's build will be optimized by default.Hierology
@legends2k: RVO is too useful to be left to the users' whims. Further, this is one of the very few instances where the standard allows an optimization. That reinvigorates why it is left on. However, note that this is not true of other optimizations in general.Covenantor
I
4

This is an example of Return Value Optimization(RVO) features which your compiler supports.

A copy constructor might not be called when you return by value.

Use -fno-elide-constructors option on GCC to turn that feature off.

Imbed answered 24/2, 2010 at 2:18 Comment(0)
I
2

I believe it's called return value optimization.

I assume when f() returns C object the object is allocated in the stack space of the calling method therefore no copy is required to initialize C a. This is your default C called.

C b = a

This causes a copy constructor hence your CC called.

Btw, the example on wiki looks quite like similar to your code.

Ikkela answered 24/2, 2010 at 2:24 Comment(1)
+1 for the wiki link. Oops! Even the naming looks similar, but I swear I didn't post after reading that, I'm reading Thinking in C++ :)Hierology

© 2022 - 2024 — McMap. All rights reserved.