c++ - 如何使用可变参数包为嵌套模板类起别名

标签 c++ c++11 templates c++14 variadic-templates

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

相关文章:

c++ - 使用 Asio::read 读取单字节

C++14 编译时 std::array 与可变参数模板

c++ - C++ 中的原子操作

php - 创建用于呈现公共(public)页面的自定义模板

c++ - 使用模板的频率函数

c++ - std::map:当元素不可默认构造时创建/替换元素

c++ - ESRI shapefile 选择哪个 C++ 库?

c++ - 如何通过我的 C++ 代码读取和传递 svn 版本

c++ - 这个奇怪的复制构造函数错误在提示什么?

c++ - 嵌套在其他任何类别中的已知类名称的C++模板 friend 声明