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
#include <vector>
#include <string>
#include <iostream>
#include
Matt Hortman 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?
The short answer is that operator<< for N::X will will only work reliably if you place it in namespace N. In your case this means placing it in namespace std. Adding overloads to namespace std is forbidden by the standard, but is the only way that will work on compilers that implement standard-conforming name lookup in templates.
On 4/13/05, Matt Hortman
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
participants (3)
-
Matt Hortman
-
Peter Dimov
-
Stuart Dootson