boost::exception が含まれている場合、2 レベルのバリアント構造体で奇妙な問題が発生します。次のコード スニペットがあります。
#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> ]
コード スニペットは可能な限り単純化されていますが、実際のコードの構造ははるかに複雑で、各バリアントには 5 つのサブタイプがあります。
#include boost/exception/all を削除して次のテスト スニペットを試すと、プログラムは正しくコンパイルされます。
void TestVariant()
{
ExpressionItem test;
std::stringstream str;
str << test;
}
boost::Exception を使用している場合でも機能するために、演算子 << を定義する方法を教えてください。
感謝と敬意
リック