Sunday, 15 September 2013

Why can't the complier find this operator

Why can't the complier find this operator

I'm trying to write overloads of operator<< for specific instantiations of
standard library containers that will be stored in a boost::variant.
Here's a small example that illustrates the problem:
#include <iostream>
#include <vector>
std::ostream & operator<<( std::ostream & os, const std::vector< int > & ) {
os << "Streaming out std::vector< int >";
return os;
}
std::ostream & operator<<( std::ostream & os, const std::vector< double >
& ) {
os << "Streaming out std::vector< double >";
return os;
}
#include <boost/variant.hpp>
typedef boost::variant< std::vector< int >, std::vector< double > >
MyVariant;
int main( int argc, char * argv[] ) {
std::cout << MyVariant();
return 0;
}
Clang's first error is
boost/variant/detail/variant_io.hpp:64:14: error: invalid operands to
binary expression ('std::basic_ostream<char>' and 'const std::vector<int,
std::allocator<int>>')
out_ << operand;
~~~~ ^ ~~~~~~~
I realize that the #include <boost/variant.hpp> is in an odd place. I'm
pretty sure the problem had to do with two-phase name lookup in templates,
so I moved the #include in an attempt to implement fix #1 from the clang
documentation on lookup. Fix #2 from that documentation isn't a good
option because I believe adding my overloaded operator<< to the std
namespace would lead to undefined behavior.
Shouldn't defining my operator<<s before the #include allow the compiler
to find the definitions?

No comments:

Post a Comment