I am interested in the correct semantics of C++ when the -fno-elide-constructors option is used with clang or gcc, and an object is being returned by value. If I have this function and call it:
X foo()
{
X x(0);
X y(1);
X z;
if (rand() > 50)
z = x;
else
z = y;
return z;
}
X z = foo();
then I can see that the copy constructor of X is called only once. However, if I modify the function slightly like so:
X foo()
{
X x(0);
X y(1);
if (rand() > 50)
return x;
else
return y;
}
X z = foo();
then the copy constructor is called twice. I can see how this might necessary in the latter case from an implementation perspective but what I find confusing is that it seems that even when we explicitly switch off copy elision there are different numbers of copy constructors being called depending on how the function is implemented.
Is there a section of the standard which covers this behaviour?