fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct SomeObject
  5. {
  6. SomeObject() : value(0), handle(0) {}
  7.  
  8. SomeObject(const SomeObject& o) : value(o.value), handle(o.handle) {
  9. std::cout << "Copy construct from " << this << " to " << &o;
  10. }
  11.  
  12. SomeObject(SomeObject&& o) : value(o.value), handle(o.handle) {
  13. std::cout << "Move construct from " << this << " to " << &o;
  14. }
  15.  
  16. void operator=(SomeObject const &o)
  17. {
  18. std::cout << "Copy from " << this << " to " << &o;
  19. }
  20. void operator=(SomeObject &&o)
  21. {
  22. std::cout << "Move from " << this << " to " << &o;
  23. }
  24.  
  25. /* example data */
  26. int value;
  27. int handle;
  28.  
  29. };
  30.  
  31. SomeObject copy_maybe(bool make_new, const SomeObject& def)
  32. {
  33. if (make_new)
  34. return SomeObject();
  35. else
  36. return def;
  37. }
  38.  
  39. int main() {
  40. SomeObject obj;
  41.  
  42. obj = copy_maybe(true, obj);
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 4396KB
stdin
Standard input is empty
stdout
Move from 0x7ffe67a3d7b0 to 0x7ffe67a3d7c0