Consider the following code:
#include
using namespace std;
class A
{
public:
int a;
A(): a(5)
{
cout &l
The copy that is elided is the copy of the temporary return value into b
. Without elision the return value is initialized from a
and copied to b
. Instead, the temporary that would otherwise hold the return value is constructed into b
and initialized with a
. [class.copy]/31:
when a temporary class object that has not been bound to a reference (12.2) would be copied/moved to a class object with the same cv-unqualified type, the copy/move operation can be omitted by constructing the temporary object directly into the target of the omitted copy/move
You can observe this if you add an additional output in fun
:
A fun(A a)
{
cout << "fun!" << endl;
return a;
}
Then with the elision you'll get
[…]
fun!
Copy Constructor
And without:
[…]
fun!
Copy Constructor
Copy Constructor