0

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 を使用している場合でも機能するために、演算子 << を定義する方法を教えてください。

感謝と敬意

リック

4

1 に答える 1

0

ブースト::例外とは何の関係もないと思います。出力ストリーム「演算子 <<」です。しかし、私はあなたが使用しているようにバリアントを使用していません.1つのタイプのみです。これは「ステロイドの組合」であるため、少なくとも2つのタイプがあるはずだと思いましたが、暗黙の何かがあるかもしれません...ドキュメントを再検討します。

あなたのコードはブースト名前空間内にありますか? あなたの出力ストリーム演算子は、例外用に定義されたものと衝突すると思います。コードを独自の名前空間に配置してみてください。

実行されていない演算子に関しては、間違ったものを選択するという問題が残っている可能性があります... std::stringstream で行うように、名前空間と解決演算子を前に付けて「<<」演算子を使用してみてください。

編集:最後のコメントのフォローアップとして: 独自の名前空間で演算子を定義できます。たとえば、mynamespaceを使用して、必要に応じてバージョンを明示的に使用できます。

void TestVariant()
{
  using namespace mynamespace;

  ExpressionItem test;
  std::stringstream str;
  str << test;
}

上記の例で動作しますが、それがあなたが直面している正確な状況であるかどうかはわかりません...そして私は精神にあまり詳しくありません

于 2011-03-13T08:25:00.390 に答える