fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct CtorDefParam {
  5. CtorDefParam() {
  6. cout << __FUNCTION__ << " - def.ctor! " << std::hex << static_cast<void*>(this) << endl;
  7. }
  8.  
  9. CtorDefParam(CtorDefParam const&
  10. , int dummy=0
  11. ) {
  12. cout << __FUNCTION__ << " - cpy.ctor! " << std::hex << static_cast<void*>(this) << endl;
  13. }
  14. };
  15.  
  16. CtorDefParam NRVO() {
  17. CtorDefParam obj;
  18. return obj;
  19. }
  20.  
  21. CtorDefParam RVO() {
  22. return CtorDefParam();
  23. }
  24.  
  25. int main()
  26. {
  27. cout << "# Init Object (should elide):\n";
  28. auto obj1 = CtorDefParam(); // copied once!
  29. cout << "# Init obj from function return value (should elide via NRVO):\n";
  30. auto obj2 = NRVO(); // copied twice!!!
  31. cout << "# Init obj from function return value (should elide via RVO):\n";
  32. auto obj3 = RVO(); // copied twice!!!
  33. return 0;
  34. }
  35.  
  36.  
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