【问题标题】:Get the Nth type of variadic template templates?获取第 N 种可变参数模板模板?
【发布时间】:2013-04-11 15:42:04
【问题描述】:

如何获取第N种可变参数模板模板?例如

template<typename... Args>
class MyClass
{
    Args[0] mA; // This is wrong. How to get the type?

};

【问题讨论】:

  • 检查 std::tuple 实现(使用与模板混合的继承)

标签: c++ variadic-templates


【解决方案1】:

你可以使用std::tuple:

#include<tuple>

template<typename... Args>
class MyClass
{
    typename std::tuple_element<0, std::tuple<Args...> >::type mA;
};

【讨论】:

    【解决方案2】:

    如果你想要一些东西而不使用std::tuple,这可行

    template<std::size_t N, typename T, typename... types>
    struct get_Nth_type
    {
        using type = typename get_Nth_type<N - 1, types...>::type;
    };
    
    template<typename T, typename... types>
    struct get_Nth_type<0, T, types...>
    {
        using type = T;
    };
    

    template<std::size_t N, typename... Args>
    using get = typename get_Nth_type<N, Args...>::type;
    
    template<typename... Args>
    class MyClass
    {
        get<0, Args...> mA;
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2016-12-01
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2013-09-14
      相关资源
      最近更新 更多