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

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

should return my something like a touple to bases types. How can I get the nth element of this __reflection_typelist<...>. I search for something like tuple_element for the reflection list.

einpoklum
  • 118,144
  • 57
  • 340
  • 684
Klaus
  • 24,205
  • 7
  • 58
  • 113

3 Answers3

3

You can use this simple metafunction to turn the typelist into an 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;
};

At this point, you could work with it as you would normally work with a tuple. For instance, this is how you could retrieve elements of the type list:

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
}
Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • Thanks for that simplification! I wonder why tr2 do not use tuple itself. Would make the things more stringent. – Klaus Mar 01 '13 at 13:29
  • 1
    1) There is no TR2, this stuff is in transition 2) See the explanation at http://gcc.gnu.org/ml/gcc-patches/2011-09/msg01789.html – Jonathan Wakely Mar 01 '13 at 15:51
1

Write your own?

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;
};

Or even using 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;
};

At this point, I'd say the source code is the best documentation.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • +1 for the `first` / `next` example. I'd add `static_assert( !R::empty::value, "Cannot get element of empty typelist" )` or use `std::conditional` – Jonathan Wakely Mar 01 '13 at 15:56
0

Since the above examples don't seem to run out of the box I thought I might add the little Codesnippet I figured out to retrieve the first baseclass-type of my type T:

if constexpr (!std::tr2::direct_bases<T>::type::empty::value)
{
    typedef typename std::tr2::direct_bases<T>::type::first::type BaseType0;
}

The if constexpr here is there to handle the case that my type T has no baseclasses, otherwise the tepedef won't work. According to the source code the next baseclass-type can be retrieved from std::tr2::direct_bases<T>::type::rest, which again has a type empty which you can use to check if another basetype exists and a type first, which holds the next type... In this manner it's possible to traverse the list of direct bases.

DocValle
  • 125
  • 2
  • 10