2

バリアントと構造に対するboost::static_visitorの適用について少し混乱しています。以下にテストケースを含めました。「s_visitor」のコメントアウトされたセクションについて、次のエラーメッセージが表示される理由または修正方法がわかりません。

apply_visitor_unary.hpp:72:エラー:「structs1」には「apply_visitor」という名前のメンバーがありません</ p>

#include "boost/variant.hpp"
#include "iostream"

struct s1 {
  int val;

  s1(int a) : val(a) {}
};

struct s2 {
  s1  s;
  int val;

  s2(int a, int b) : s(a), val(b) {}
};

struct s_visitor : public boost::static_visitor<>
{
    void operator()(int & i) const
    {
        std::cout << "int" << std::endl;
    }

    void operator()(s1 & s) const
    {
        std::cout << "s1" << std::endl;
    }

    void operator()(s2 & s) const
    {
        std::cout << "s2" << std::endl;
        // -> following 'struct s1' has no member apply_visitor
        // boost::apply_visitor(s_visitor(), s.s);
        // -> following 'struct s1' has no member apply_visitor
        // boost::apply_visitor(*this, s.s);
        s_visitor v;
        v(s.s);
    }
};

int main(int argc, char **argv)
{
  boost::variant< int, s1, s2 > v;
  s1 a(1);
  s2 b(2, 3);

  v = a;
  boost::apply_visitor(s_visitor(), v);

  v = b;
  boost::apply_visitor(s_visitor(), v);

  return 0;
}

助けや説明をありがとう。

4

1 に答える 1

1

boost :: Variantが期待される「s1」を渡すため、コメント化された両方の行でコンパイルエラーが発生します。ただし、コードのその時点では正確なタイプがわかっているため、バリアント訪問を行う必要はなく、タイプs1の値を使用して好きなことを行うことができます。

于 2011-04-28T20:31:27.897 に答える