3

boost::variant を比較して std::map のキーにする方法は? boost::variant に operator<() が定義されていないようです

4

2 に答える 2

5

BOOST::APPLY_VISITOR の適用エラーを修正するために編集

バリアントのバイナリ ビジターを作成し、boost::apply_visitor を使用してマップのコンパレータを作成できます。

class variant_less_than
    : public boost::static_visitor<bool>
{
public:

    template <typename T, typename U>
    bool operator()( const T & lhs, const U & rhs ) const
    {
            // compare different types
    }

    template <typename T>
    bool operator()( const T & lhs, const T & rhs ) const
    {
            // compare types that are the same
    }

};

テンプレートoperator()化されoperator(const T &, const U &)た . 次に、次のようにマップを宣言する必要があります。

class real_less_than
{
public:
  template<typename T>
  bool operator()(const T &lhs, const T &rhs)
  {
    return boost::apply_visitor(variant_less_than(), lhs, rhs);
  }
};

std::map<boost::variant<T, U, V>, ValueType, real_less_than> myMap;

編集:それが価値があるもののためにoperator<()定義されていますが、boost::variant次のように定義されています:

bool operator<(const variant &rhs) const
{
  if(which() == rhs.which())
    // compare contents
  else
    return which() < rhs.which();
}

私が想定しているのは、あなたが望むものではありません。

于 2010-12-03T23:05:37.723 に答える
1

おそらく、コンパレータをマップに渡すことができます。コンパレータの書き方の例については、http://www.sgi.com/tech/stl/Map.htmlを参照してください。

于 2010-12-03T22:57:35.383 に答える