fork download
#include <iostream>
using namespace std;

struct CtorDefParam {
	CtorDefParam() {
		cout << __FUNCTION__ << " - def.ctor! " << std::hex << static_cast<void*>(this) << endl;
	}
	
	CtorDefParam(CtorDefParam const&
		, int dummy=0
	) {
		cout << __FUNCTION__ << " - cpy.ctor! " << std::hex << static_cast<void*>(this) << endl;
	}
};

CtorDefParam NRVO() {
	CtorDefParam obj;
	return obj;
}

CtorDefParam RVO() {
	return CtorDefParam();
}

int main()
{
	cout << "# Init Object (should elide):\n";
	auto obj1 = CtorDefParam(); // copied once!
	cout << "# Init obj from function return value (should elide via NRVO):\n";
	auto obj2 = NRVO(); // copied twice!!!
	cout << "# Init obj from function return value (should elide via RVO):\n";
	auto obj3 = RVO(); // copied twice!!!
	return 0;
}

Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
# Init Object (should elide):
CtorDefParam - def.ctor! 0xbff3256d
# Init obj from function return value (should elide via NRVO):
CtorDefParam - def.ctor! 0xbff3256e
# Init obj from function return value (should elide via RVO):
CtorDefParam - def.ctor! 0xbff3256f