I have the following lines:
using par = std::pair<std::string, unsigned>;
std::istream& operator>>( std::istream&is, par & p ) {
is >> p.first >> p.second;
return is;
}
std::ostringstream out( "qwe 123\nasd 234", std::ios::out | std::ios::in );
std::istream in( out.rdbuf() );
std::copy( std::istream_iterator<par>( in ), std::istream_iterator<par>(),
std::ostream_iterator<par>( std::cout, "\t" ) );
And it doesn't work for me. What is the problem with this algorithm? I leave a link to a web editor and compiler. link to example
The problem is you
std::istream& operator>>( std::istream&is, par & p )
are not in the std namespace, or in any of the associated namespaces, which is where it will be searched following the rules of the "Argument-dependent lookup" [ADL] ( http://en.cppreference.com/ w/cpp/language/adl )There is a trick that allows the ADL mechanism to find your overloaded operator, using its own type:
Putting P in place of even, it has to work. Something like that: