我正在尝试为将存储在
我意识到
boost::variant
中的特定标准库容器实例编写operator<<
的重载。以下是一个小例子,说明了问题:#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;
}
< p > Clang 的第一个错误是 < /p >
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;
~~~~ ^ ~~~~~~~
我意识到
#include <boost/variant.hpp>
的位置很奇怪。我相当肯定问题与模板中的两阶段名称查找有关,因此我移动了 #include
以尝试实施clang文档中有关查找的修复方法1。该文档中的修复方法2不是一个好选择,因为我认为将我的重载运算符<< 添加到std命名空间会导致未定义行为。
在 #include
前定义我的 operator<<
是否应该允许编译器找到定义?从同一clang页面改编的以下示例似乎可以使用该技术。
#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> {};
,并对另一个向量执行类似操作,然后代码就能按照 OP 的要求运行,因为这些类型位于全局命名空间中。 - TemplateRextemplate <typename T> struct myvector: std::vector<int> { using std::vector<T>::vector; };
使类型变得相当完整,因为using
声明会继承std::vector<T>
的构造函数。 - Dietmar Kühlmyvector<int>
肯定会有帮助。 - Dietmar Kühl