Name Lookup - operator>> Not Found

Viewed 62

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)

}
1 Answers

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.

Related