有什么方法可以使用using关键字为嵌套的模板类添加别名?像这样

template <typename... Types>
struct Something {
    template <typename... TypesTwo>
    struct Another {};
};

template <typename... Types>
template <typename... TypesTwo>
using Something_t = typename Something<Types...>::template Another<TypesTwo...>;

int main() {
    Something_t<int><double>{};
    return 0;
}

这个答案template template alias to a nested template?显示了一种方法,但是如果两个参数包都是可变参数,该方法将不再起作用,因为编译器将不知道从何处开始和在何处结束类型列表。

最佳答案

不完全是您的要求,但是...是否可以将可变参数类型列表包装为元组(或类似类)的参数...

#include <tuple>

template <typename ... Types>
struct Something
 {
   template <typename ... TypesTwo>
   struct Another {};
 };

template <typename, typename>
struct variadicWrapper;

template <typename ... Ts1, typename ... Ts2>
struct variadicWrapper<std::tuple<Ts1...>, std::tuple<Ts2...>>
 { using type = typename Something<Ts1...>::template Another<Ts2...>; };

template <typename T1, typename T2>
using Something_t = typename variadicWrapper<T1, T2>::type;

int main()
 {
   Something_t<std::tuple<int>, std::tuple<double>>{};
 }

关于c++ - 如何使用可变参数包为嵌套模板类添加别名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42815010/

10-11 22:38
查看更多