使用tr2 :: direct_bases获取结果的第n个元素 [英] using tr2::direct_bases get nth element of result
本文介绍了使用tr2 :: direct_bases获取结果的第n个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
struct T1 {};
struct T2: T1 {};
typedef tr2::direct_bases<T2>::type NEW_TYPE ;
应该将我的类似返回给基本类型。如何获得此__reflection_typelist< ...>的第n个元素
。我在反射列表中搜索类似tuple_element的东西。
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.
推荐答案
您可以使用此简单的元函数将类型列表转换为 std :: tuple
:
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
}
这篇关于使用tr2 :: direct_bases获取结果的第n个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文