fork(1) download
  1. #include <iostream>
  2.  
  3. class Int
  4. {
  5. int n_;
  6.  
  7. public:
  8. explicit Int(int n) noexcept : n_(n) { std::cout << "Int(" << n_ << ")\n"; }
  9.  
  10. Int(Int const & rhs) : n_(rhs.n_) { std::cout << "Int(Int const &)[" << n_ << "]\n"; }
  11. Int(Int && rhs) : n_(rhs.n_) { std::cout << "Int(Int &&) [" << n_ << "]\n"; }
  12.  
  13. ~Int() { std::cout << "~Int()[" << n_ << "]\n"; }
  14.  
  15. Int & operator+=(Int const & rhs) { n_ += rhs.n_; return *this; }
  16. Int & operator+=(Int && rhs) { n_ += rhs.n_; return *this; }
  17.  
  18. #ifdef LAZY
  19. Int operator+(Int rhs) { return Int(*this) += std::move(rhs); }
  20. #else
  21. Int operator+(Int const & rhs) { Int result(*this); result += rhs; return result; }
  22. Int operator+(Int && rhs) { Int result(*this); result += std::move(rhs); return result; }
  23. #endif
  24. };
  25.  
  26.  
  27. int main()
  28. {
  29. auto x = Int(10) + Int(20) + Int(30);
  30. }
  31.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
Int(30)
Int(20)
Int(10)
Int(Int const &)[10]
Int(Int const &)[30]
~Int()[30]
~Int()[10]
~Int()[20]
~Int()[30]
~Int()[60]