fork download
  1. #include <iostream>
  2.  
  3. namespace ex
  4. {
  5. struct Test
  6. {
  7. };
  8.  
  9. std::istream &operator>>(std::istream &is, Test &t)
  10. {
  11. std::cout << "Example namespace" << std::endl;
  12. return is;
  13. }
  14. }
  15.  
  16. void f(ex::Test &t);
  17.  
  18. int main()
  19. {
  20. ex::Test t;
  21. std::cin >> t;
  22. f(t);
  23. }
  24.  
  25. std::istream &operator>>(std::istream &is, ex::Test &t)
  26. {
  27. std::cout << "Global namespace" << std::endl;
  28. return is;
  29. }
  30.  
  31. void f(ex::Test &t)
  32. {
  33. std::cin >> t; //error: ambiguous
  34. }
  35.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘void f(ex::Test&)’:
prog.cpp:33:17: error: ambiguous overload for ‘operator>>’ in ‘std::cin >> t’
prog.cpp:33:17: note: candidates are:
prog.cpp:25:15: note: std::istream& operator>>(std::istream&, ex::Test&)
In file included from /usr/include/c++/4.7/iostream:41:0,
                 from prog.cpp:1:
/usr/include/c++/4.7/istream:866:5: note: std::basic_istream<_CharT, _Traits>& std::operator>>(std::basic_istream<_CharT, _Traits>&&, _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = ex::Test] <near match>
/usr/include/c++/4.7/istream:866:5: note:   no known conversion for argument 1 from ‘std::istream {aka std::basic_istream<char>}’ to ‘std::basic_istream<char>&&’
prog.cpp:9:19: note: std::istream& ex::operator>>(std::istream&, ex::Test&)
stdout
Standard output is empty