fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class test {
  5. public:
  6. test(int input = 0)
  7. : value(input)
  8. {
  9. std::cout << "default constructor called with " << value << "\n";
  10. }
  11.  
  12. test(const test& other)
  13. : test(other.value)
  14. {
  15. std::cout << "copy constructor called with " << value << "\n";
  16. }
  17.  
  18. test(test&& other) noexcept
  19. : test()
  20. {
  21. std::swap(value, other.value);
  22. std::cout << "move constructor called with " << value << "\n";
  23. }
  24.  
  25. test& operator=(test other)
  26. {
  27. std::swap(value, other.value);
  28. std::cout << "copy assignment called with " << value << "\n";
  29. return *this;
  30. }
  31. /*
  32. test& operator=(const test& other)
  33. {
  34. test tmp(other);
  35. std::swap(value, tmp.value);
  36. std::cout << "copy assignment called with " << value << "\n";
  37. return *this;
  38. }
  39. */
  40.  
  41. test& operator=(test&& other)
  42. {
  43. std::swap(value, other.value);
  44. std::cout << "move assignment called with " << value << "\n";
  45. return *this;
  46. }
  47.  
  48. int value;
  49. };
  50.  
  51. int main() {
  52. test myTest1 = std::move(test(1));
  53. test myTest2 = test(2);
  54. myTest1 = myTest2;
  55. myTest1 = std::move(myTest2);
  56. // your code goes here
  57. return 0;
  58. }
Compilation error #stdin compilation error #stdout 0s 4456KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:55:29: error: ambiguous overload for ‘operator=’ (operand types are ‘test’ and ‘std::remove_reference<test&>::type {aka test}’)
  myTest1 = std::move(myTest2);
                             ^
prog.cpp:25:8: note: candidate: test& test::operator=(test)
  test& operator=(test other)
        ^~~~~~~~
prog.cpp:41:8: note: candidate: test& test::operator=(test&&)
  test& operator=(test&& other)
        ^~~~~~~~
stdout
Standard output is empty