当包含boost :: exception时,我有两级变量struct的奇怪问题。我有下面的代码片段:组合boost :: exception和boost :: variant的问题
#include <boost/variant.hpp>
#include <boost/exception/all.hpp>
typedef boost::variant<int> StoredValue;
typedef boost::variant<StoredValue> ExpressionItem;
inline std::ostream& operator << (std::ostream & os, const StoredValue& stvalue) { return os;}
inline std::ostream& operator << (std::ostream & os, const ExpressionItem& stvalue) { return os; }
当我尝试编译它,我有以下错误:
boost/exception/detail/is_output_streamable.hpp(45): error C2593: 'operator <<' is ambiguous
test.cpp(11): could be 'std::ostream &operator <<(std::ostream &,const ExpressionItem &)' [found using argument-dependent lookup]
test.cpp(8): or 'std::ostream &operator <<(std::ostream &,const StoredValue &)' [found using argument-dependent lookup]
1> while trying to match the argument list '(std::basic_ostream<_Elem,_Traits>, const boost::error_info<Tag,T>)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> and
1> [
1> Tag=boost::tag_original_exception_type,
1> T=const type_info *
1> ]
代码片断简化尽可能在实际的代码是结构要复杂得多并且每个变体都有五个子类型。
当我删除的#include升压/异常/所有,并尝试下面的测试片段中,程序正确编译:
void TestVariant()
{
ExpressionItem test;
std::stringstream str;
str << test;
}
可能有人请告诉我如何使用时,以偶函数定义操作符< < boost :: Exception?
感谢和问候
里克
我不认为这与boost :: exception有什么关系。这是输出流“operator <<”。但是我没有使用这个变体,因为你正在使用它 - 只有一种类型;我以为你应该至少有两种类型,因为这是一个“类固醇联合”,但也许有一些隐含的...我会重新访问文档。 – celavek 2011-03-13 00:02:00
单一类型仅用于示例目的。在我的真实代码中,每个变体都有五个子类型。如果没有boost :: exception,包括一切正常。 – Rick 2011-03-13 00:06:15
你的代码是否在boost命名空间中?我认为你的输出流操作符与为异常定义的操作符冲突。尝试把你的代码放在你自己的名字空间中。 – celavek 2011-03-13 00:13:50