fork download
  1. #include <iostream>
  2.  
  3. class Foo {
  4. public:
  5. Foo() = default;
  6.  
  7. Foo(const Foo&) = default;
  8. Foo(Foo&&) noexcept = default;
  9.  
  10. Foo& operator=(const Foo& rhs) & {
  11. std::cout << "copy\n";
  12.  
  13. return *this;
  14. }
  15.  
  16. Foo& operator=(Foo&& rhs) & noexcept {
  17. std::cout << "move\n";
  18.  
  19. return *this;
  20. }
  21.  
  22. Foo operator+(const Foo& rhs) const {
  23. Foo x; // with some calculation
  24.  
  25. return x;
  26. }
  27. };
  28.  
  29. int main() {
  30. Foo a, b, c;
  31.  
  32. a + b = c;
  33. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:32:13: error: passing ‘Foo’ as ‘this’ argument discards qualifiers [-fpermissive]
     a + b = c;
             ^
prog.cpp:10:10: note:   in call to ‘Foo& Foo::operator=(const Foo&) &’
     Foo& operator=(const Foo& rhs) & {
          ^~~~~~~~
stdout
Standard output is empty