Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-06-26 13:56:18


Ovanes Markarian wrote:

> Sorry,
>
> you are right. I forgot that all operators are overloaded or defined
> in std namespace.

It's a bit more subtle than that. As a general rule, an operator needs to be
defined in the namespace of the class for which it applies. If you had

std::ostream& operator<<( std::ostream& os, my::X const & x );

you'd need to put this operator in namespace my, because that's where the
class my::X resides.

All this assumes that your compiler supports argument-dependent lookup
(ADL); for older compilers, the rule is "put the operator wherever it
appears to work."

However, since the operator you are defining:

template <typename T>
std::ostream& operator<<( std::ostream& os, const std::vector<T>& v );

applies to std::vector, it needs to be defined in namespace std.

In principle, a class N::X is supposed to also define the necessary support
operators in namespace N, but std::vector doesn't do that (mainly because it
isn't clear how is this operator supposed to work; there are many legitimate
ways to output a sequence.)

This is a general C++ problem, not specific to Boost.Variant, so it's
understandable why the documentation doesn't mention it.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net