bzs
bzs

Reputation: 509

BOOST unittest override operator<<

To test two std::pair

BOOST_CHECK_EQUAL(std::make_pair(0.0,0.0), std::make_pair(1.0,1.0));

I overloads the operator<< for std::pair

std::ostream& operator<< (std::ostream& os, const std::pair<double,double>& t)
{
  return os << "( " << t.first << ", " << t.second << ")"; 
}

with following error

error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'const std::pair<_Ty1,_Ty2>' (or there is no acceptable conversion)

What is wrong?

Upvotes: 0

Views: 864

Answers (1)

Jesse Good
Jesse Good

Reputation: 52365

Open up the std namespace so ADL can find it.

namespace std
{
ostream& operator<< (ostream& os, const pair<double,double>& t)
{
  return os << "( " << t.first << ", " << t.second << ")"; 
}
}

Ok, I figured it out. Name lookup stops when it finds the name it is looking for in the current namespace, which is why it cannot find your operator<< in global scope, since it found operator<< already in namespace boost because boost declares operator<<.

I recommend reading Why can't I instantiate operator<<(ostream&, vector&) with T=vector? which has a good explanation.

Upvotes: 3

Related Questions