fork download
  1. #include <iostream>
  2.  
  3. class foo {
  4. public:
  5. foo() : i(1), j(2) {}
  6.  
  7. int i;
  8. int j;
  9. bool operator<(const foo& val) const {
  10. std::cout << "memba\n";
  11. return i<val.i;
  12. }
  13. };
  14.  
  15. bool operator<(const foo& lhs, const foo& rhs) {
  16. std::cout << "free\n";
  17. return lhs.j < rhs.j;
  18. }
  19.  
  20. int main()
  21. {
  22. foo f1, f2;
  23. bool b1 = f1 < f2;
  24.  
  25. foo const f3;
  26. bool b2 = f3 < f2;
  27.  
  28. return b1 + b2;
  29. }
  30.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main()':
prog.cpp:23:20: warning: ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second:
prog.cpp:15:6: note: candidate 1: bool operator<(const foo&, const foo&)
prog.cpp:9:9: note: candidate 2: bool foo::operator<(const foo&) const
prog.cpp:26:20: error: ambiguous overload for 'operator<' in 'f3 < f2'
prog.cpp:9:9: note: candidates are: bool foo::operator<(const foo&) const
prog.cpp:15:6: note:                 bool operator<(const foo&, const foo&)
stdout
Standard output is empty