c++ - 使用 tr2::direct_bases 获取结果的第 n 个元素

标签 c++ c++11 c++-tr2

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/

相关文章:

c++ - CMake 中的 include_directory 继承?

c++ - 使用 std::wcout 会降低奇怪的性能

c++ - 如何返回 `set` 方法的子类类型?

c++ - 无法使用std::greater,其中使用了std::less

c++ - N2965 - std::bases 和 std::direct_bases 的状态如何?

c++ - 如何从二进制文件安装 memcached 并包含在 c 文件中

自定义类对象的 C++ vector - 复制构造函数已删除 - std::ifstream

c++ - std::shared_ptr 指向的对象何时被删除?

用于 VS2005 的 C++ std::tr2