fork(1) download
  1. #include <iostream>
  2. template<typename Ty>
  3. struct Foo
  4. {
  5. Foo& operator=(Foo foo){ return foo; }
  6. private:
  7. Foo& operator=(Foo& foo){ return foo; }
  8. };
  9. template<typename Ty>
  10. struct Bar
  11. {
  12. template<typename U>
  13. Bar& operator=(Bar<U> bar){ return bar; }
  14. private:
  15. Bar& operator=(Bar& bar){ return bar; }
  16. };
  17.  
  18. int main()
  19. {
  20. Foo<int> f1, f2;
  21. f1 = f2; // (1)
  22. Bar<int> b1, b2;
  23. b1 = b2; // (2)
  24. f1 = Foo<int>(f2); // (3)
  25. b1 = Bar<int>(b2); // (4)
  26. }
Compilation error #stdin compilation error #stdout 0s 3340KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:21:5: error: ambiguous overload for ‘operator=’ (operand types are ‘Foo<int>’ and ‘Foo<int>’)
  f1 = f2;   // (1)
     ^
prog.cpp:21:5: note: candidates are:
prog.cpp:5:7: note: Foo<Ty>& Foo<Ty>::operator=(Foo<Ty>) [with Ty = int]
  Foo& operator=(Foo foo){ return foo; } 
       ^
prog.cpp:7:7: note: Foo<Ty>& Foo<Ty>::operator=(Foo<Ty>&) [with Ty = int]
  Foo& operator=(Foo& foo){ return foo; }
       ^
prog.cpp:15:7: error: ‘Bar<Ty>& Bar<Ty>::operator=(Bar<Ty>&) [with Ty = int]’ is private
  Bar& operator=(Bar& bar){ return bar; }
       ^
prog.cpp:23:5: error: within this context
  b1 = b2;   // (2)
     ^
prog.cpp: In instantiation of ‘Foo<Ty>& Foo<Ty>::operator=(Foo<Ty>) [with Ty = int]’:
prog.cpp:24:5:   required from here
prog.cpp:5:7: warning: reference to local variable ‘foo’ returned [-Wreturn-local-addr]
  Foo& operator=(Foo foo){ return foo; } 
       ^
prog.cpp: In instantiation of ‘Bar<Ty>& Bar<Ty>::operator=(Bar<U>) [with U = int; Ty = int]’:
prog.cpp:25:5:   required from here
prog.cpp:13:7: warning: reference to local variable ‘bar’ returned [-Wreturn-local-addr]
  Bar& operator=(Bar<U> bar){ return bar; }
       ^
stdout
Standard output is empty