2
struct T1 {};
struct T2: T1 {};

typedef tr2::direct_bases<T2>::type NEW_TYPE ;

toupleのようなものをbasesタイプに戻す必要があります。この__reflection_typelist<...>のn番目の要素を取得するにはどうすればよいですか。リフレクションリストのtuple_elementのようなものを検索します。

4

2 に答える 2

3

この単純なメタ関数を使用して、タイプリストを次のように変換できますstd::tuple

#include <tr2/type_traits>
#include <tuple>

template<typename T>
struct dbc_as_tuple { };

template<typename... Ts>
struct dbc_as_tuple<std::tr2::__reflection_typelist<Ts...>>
{
    typedef std::tuple<Ts...> type;
};

この時点で、通常のタプルと同じように使用できます。たとえば、これは型リストの要素を取得する方法です:

struct A {};
struct B {};
struct C : A, B {};

int main()
{
    using namespace std;

    using direct_base_classes = dbc_as_tuple<tr2::direct_bases<C>::type>::type;

    using first = tuple_element<0, direct_base_classes>::type;
    using second = tuple_element<1, direct_base_classes>::type;

    static_assert(is_same<first, A>::value, "Error!");   // Will not fire
    static_assert(is_same<second, B>::value, "Error!");  // Will not fire
}
于 2013-03-01T13:17:06.463 に答える
1

自分で書く?

template <typename R, unsigned int N> struct get;

template <typename T, typename ...Args, unsigned int N>
struct get<std::tr2::__reflection_typelist<T, Args...>, N>
{
    typedef typename get<std::tr2::__reflection_typelist<Args...>, N - 1>::type type;
};

template <typename T, typename ...Args>
struct get<std::tr2::__reflection_typelist<T, Args...>, 0>
{
    typedef T type;
};

またはfirst/を使用することもできnextます:

template <typename R, unsigned int N>
struct get
{
    typedef typename get<typename R::next::type, N - 1>::type type;
};

template <typename R>
struct get<R, 0>
{
    typedef typename R::first::type type;
};

現時点では、ソース コードが最良のドキュメントであると言えます。

于 2013-03-01T13:08:59.740 に答える