當包含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