Boost logo

Boost Users :

From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2005-04-13 08:52:48


On 4/13/05, Matt Hortman <matt_at_[hidden]> wrote:
> Hello,
>
> I feel almost silly asking this but:
>
> I can only get the code listed below to compile if I remove the
> comments on the namespace statements, placing my custom operator<< in
> the boost::detail::variant namespace. Otherwise I get the error
>
> c:\Boost\include\boost-1_32\boost\variant\detail\variant_io.hpp(64) :
> error C2679: binary '<<' : no operator found which takes a right-hand
> operand of type 'const T1' (or there is no acceptable conversion)
>
> from VC++ 7.1 and a similar error from both gcc 3.3 and 3.4. I'm
> using boost 1.32 on both WinXP and FreeBSD. Looking at the code in
> variant_io.hpp, I see no reason why it should not work without the
> namespace statements. Is there something fundamental about namespaces
> that I don't understand?
>
> Thanks,
>
> -Matt

Matt - this is (as far as I understand the C++ standard) by design.
When the compiler sees

std::cout << v << std::endl;

it uses the operator<< found in the boost namespace. This function is
found because when resolving an unqualified function call, the
compiler will look for a matching function in the namespaces
containing the types of the function parameters as well as the scope
immediately containing the function call. Now, the operator<< for
variant in the boost namespace happens to delegate to code in
boost::detail::variant. This is what calls operator<< for your strlist
type.

Again, the compiler has to find a suitable operator<<. It will again
do this by looking in the immediately enclosing scope
(boost::detail::variant) and the namespaces containing any parameter
types. In this case, the parameters are std::ostream and strlist,
which is a std::vector, so the compiler will look in namespace std as
well. In fact, you're better off declaring your operator<< in
namespace std, as it will be found by 'argument dependent lookup' no
matter what namespace the calling function belongs to.

HTH

Stuart Dootson


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