Possible Duplicate:
Why copy constructor is not called in this case?
Considering the following code snippet:
#include <iostream>
using namespace std;
class Test
{
char name[16];
public:
Test ()
{
cout <<"Inside Constructor"<<endl;
}
Test (const Test & t)
{
cout <<"Inside Copy Constructor "<<endl;
}
};
Test f()
{
return Test();
}
int main ( int argc, char ** argv)
{
Test t;
Test t1 = f();
}
Test t1= f() -> it invokes f(), and which returns the Test object, and then as per my understanding the copy constructor should be invoked. But i get the following output:
Inside Constructor
Inside Constructor
what is wrong with my understanding?.
Two copy elisions occur here.
The first is a kind of return value optimization (instead of copying the result of the expression
Test()to the temporary object that is the return value off, the expressionTest()is evaluated by constructing directly into the temporary object that is the return value off).The second is the elision of the copy from the initializer expression of
t1tot1itself (so instead of copying the temporary that is the return value offintot1, the temporary that is the return value offis constructed directly intot1).The two elisions chain together — so the memory of
t1is used as the destination when constructing the return value off, and the memory for the return value offis used as the destination when constructingTest(). So in effectt1is direct-initialized by the no-args constructor and there are no copies needed.Copy constructor elision is defined in the standard in 12.8/15 of C++03 and 12.8/31 of C++11 (which also allows elision of moves). It requires specific permission because it changes the observable behavior of the program (in your case it omits the side-effect of the copy constructor, the output). So it can only be performed under the conditions defined in the standard.
Both of these elisions are examples of the second permitted elision in C++03 (third in C++11), when the source is a temporary.
The first permitted elision is often called the “named return value optimization”, and it allows a particular kind of copy to be omitted when the source is not a temporary.