3

I fail to understand the reason why the code below does not compile. Live.

Thank you.

#include <iostream>
#include <iterator>
#include <sstream>

using namespace std;

namespace N
{
  class C
  {
  public: enum class E { a, b, c };
  };
}

using U = N::C::E;

istream& operator>>( istream& is, U& e )
{
  //...
  return is;
}

int main()
{
  istringstream ss{ "0 1 2" };

  U e;
  ss >> e; // fine

  istream_iterator<U> ii( ss ); // binary '>>': no operator found which takes a right-hand operand of type '_Ty' (or there is no acceptable conversion)

}
zdf
  • 4,382
  • 3
  • 18
  • 29
  • Does this answer your question? [Why does ostream\_iterator not work as expected?](https://stackoverflow.com/questions/4447827/why-does-ostream-iterator-not-work-as-expected) – scohe001 Aug 10 '20 at 13:13

1 Answers1

3

https://en.cppreference.com/w/cpp/language/adl

Because of argument-dependent lookup, non-member functions and non-member operators defined in the same namespace as a class are considered part of the public interface of that class (if they are found through ADL)

So, move istream& operator>>( istream& is, C::E& e ) into the namespace N.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108