有什么方法可以使用 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/