struct T1 {};
struct T2: T1 {};
typedef tr2::direct_bases<T2>::type NEW_TYPE ;
应该将我的类似于的东西返回到基本类型。我怎样才能得到第n个元素 这个 __reflection_typelist<...>。我在反射列表中搜索类似 tuple_element 的内容。
最佳答案
您可以使用这个简单的元函数将类型列表转换为 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
}
关于c++ - 使用 tr2::direct_bases 获取结果的第 n 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15158458/