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? That technique seems to work in the following example, adapted from the same clang page.
#include <iostream>
namespace ns {
struct Data {};
}
std::ostream& operator<<(std::ostream& out, const ns::Data & data) {
return out << "Some data";
}
namespace ns2 {
template<typename T>
void Dump( std::ostream & out, const T & value) {
out << value;
}
}
int main( int argc, char * argv[] ) {
ns2::Dump( std::cout, ns::Data() );
}
struct vI: std::vector<int> {};
and similary for the other vector, and then the code does what the OP wants because these types live in the global namespace. – Perfervid