I tried to understand how this allocation works in c++ :
Test other = toto();
This is the full code source :
#include <iostream>
class Test
{
public:
Test()
{
j = i++;
std::cout<<"default constructor "<<j<<std::endl;
}
Test(const Test&)
{
std::cout<<"constuctor by copy "<<j<<std::endl;
}
Test & operator=(const Test&)
{
std::cout<<"operator = "<<j<<std::endl;
return *this;
}
int j;
static int i;
};
int Test::i = 0;
Test toto()
{
Test t;
return t;
}
int main()
{
Test other = toto();
std::cout<<other.j<<std::endl;
Test another;
return 0;
}
The code not used constructor by copy or operator =, so I don’t understand really how it’s works …
I used gcc 4.7.0
Thranks for your help 🙂
Jerome
The format semantics of:
involve several copies (but no assignment). The compiler is allowed to
elide all of the different instances, however, which eliminates the
copies; almost all compilers do do this optimization.
More concretely, the standard doesn’t specify where values of class type
are returned, but the usual solution is for the caller to allocate the
space, and pass a hidden pointer to it into the function. Without the
above mentionned optimizations:
would result in the local variable
tbeing constructed, then thereturn statement would copy
tinto the space pointed to by the hiddenpointer. The optimization (called named return value optimization, or
NRVO) here results in the compiler using the space pointed to by the
hidden pointer for
t, rather than creating a separatetlocally.(Obviously, when it does this, it does not destruct
t, as it wouldotherwise after the copy.)
In the declaration:
, the formal semantics would have the compiler allocate the space for a
temporary of type Test, pass the address of this space as the hidden
pointer to
toto, then copy this temporary intotand destruct it.The optimization here consists in the compiler passing the address of
tdirectly tototo, eliding the intermediate temporary.