2

C++ で型定義を書きたいのですが、実装したいものが合法かどうかわかりません。int または同じ型の別のベクトルを指すブースト バリアント型のベクトルの typedef を実行したいとします。それで、これは合法であり、コンパイラは文句を言うでしょうか?

typedef std::vector<boost::variant<int *, boost::variant<int *, IntBranch*>> IntBranch;
4

1 に答える 1

4

boost::make_recursive_variantその目的のために使用できます:

#include <boost/variant.hpp>

typedef boost::make_recursive_variant<
   int*, 
   std::vector< boost::recursive_variant_ >
>::type IntBranch;

そして、これはあなたがそれを使用する方法です:

#include <vector>

int main()
{
    typedef boost::make_recursive_variant<
       int*, 
       std::vector< boost::recursive_variant_ >
    >::type IntBranch;

    int x = 42;
    IntBranch ib = &x;

    std::vector<IntBranch> v;
    v.push_back(ib);

    IntBranch ib2 = v;

    // ...
}

そして、これがライブの例です。

于 2013-04-07T00:16:40.077 に答える