如何拥有具有类型和大小的可变参数模板
How to have variadic templates with a type and a size?
只是为了好玩,我试图重载一个struct
,一个用于std::array<T,SIZE>
,一个用于std::vector<T>
,以及一个std::unordered_map<T,U>
。所以我做了以下工作:
template<typename... T>
struct Cont;
template<typename T, std::size_t SIZE>
struct Cont<T,SIZE>
{
Cont(std::string n) : name(n){}
std::string name;
std::array<T,SIZE> array;
};
template<typename T>
struct Cont<T>
{
Cont(std::string n) : name(n){}
std::string name;
std::vector<T> vector;
};
template<typename T, typename U>
struct Cont<T,U>
{
Cont(std::string n) : name(n){}
std::string name;
std::unordered_map<T,U> unordered_map;
};
但是,当我尝试编译它时,我收到错误expected a type, got SIZE
。我完全理解这是因为typename... T
期待一种类型而不是std::size_t
.所以我尝试了:
template<std::size_t SIZE, typename... T>
struct Cont;
template<typename... T>
struct Cont;
这也行不通,因为我正在重新定义并且不像我最初认为的那样超载。我也意识到,我可以做到:
template<std::size_t SIZE, typename... T>
struct Cont;
但是,当我声明它们时,我希望能够做到:
int main()
{
Cont<int,5> myArray("myArray");
myArray.array = {1,2,3,4,5};
Cont<int> myVector("myVector");
myVector.vector = {1,2,3,4};
Cont<int,int> myMap("myMap");
myMap.unordered_map[0] = 2;
}
有没有办法重载模板?(我假设这不是(或者以一种说我要么typename... T
要么typename T, std::size_t SIZE
的方式构建模板?
我不确定如何得到你想要的。也许更聪明的用户有一个确切的解决方案。但一种替代方法可能是只为模板提供一个类型参数包,并对数值模板参数使用 std::integral_constant
s。然后,您可以专门针对每种情况。例如:
#include <array>
#include <type_traits>
#include <unordered_map>
#include <vector>
template<class...>
struct Cont;
template<class T, class U, U S>
struct Cont<T, std::integral_constant<U, S>>
{
Cont(const char *) {};
std::array<T, S> array;
};
template<class T>
struct Cont<T>
{
Cont(const char *) {};
std::vector<T> vector;
};
template<class T>
struct Cont<T, T>
{
Cont(const char *) {};
std::unordered_map<T, T> unordered_map;
};
int main()
{
Cont<int,std::integral_constant<int, 5>> myArray("myArray");
myArray.array = {1,2,3,4,5};
Cont<int> myVector("myVector");
myVector.vector = {1,2,3,4};
Cont<int,int> myMap("myMap");
myMap.unordered_map[0] = 2;
}
把它变成Cont<std::array<int, 5>>
,然后有一个template<typename T, std::size_t SIZE> struct Cont<std::array<T, SIZE>>;
的专业化,但这打破了其他类型的模式。
你可以拥有的另一件事是,除了通过将5
包装在std::integral_constant
中来使其成为类型之外,制作一对重载的帮助程序函数来自动为您完成:
template<typename... T>
Cont<T...> make_cont(std::string name) {
return { std::move(name) };
}
template<typename T, std::size_t SIZE>
Cont<T, std::integral_constant<std::size_t, SIZE>> make_cont(std::string name) {
return { std::move(name) };
}
int main() {
auto myArray = make_cont<int, 5>("myArray");
myArray.array = {1,2,3,4,5};
auto myVector = make_cont<int>("myVector");
myVector.vector = {1,2,3,4};
auto myMap = make_cont<int, int>("myMap");
myMap.unordered_map[0] = 2;
}
如果像这样声明数组版本
template <typename T, std::size_t SIZE>
struct Cont<std::array<T, SIZE>>
{
Cont(std::string n) : name(n) {}
std::string name;
std::array<T, SIZE> array;
};
那么在主要情况下,你可以像这样使用它:
int main() {
Cont<std::array<int, 5>> myArray("myArray");
myArray.array = {1, 2, 3, 4, 5};
Cont<int> myVector("myVector");
myVector.vector = {1, 2, 3, 4};
Cont<int, int> myMap("myMap");
myMap.unordered_map[0] = 2;
std::cout << "myArray " << myArray.array[0] << std::endl;
// myArray 1
std::cout << "myVector " << myVector.vector[0] << std::endl;
// myVector 1
std::cout << "myMap " << myMap.unordered_map[0] << std::endl;
// myMap 2
}
我认为这就是您正在寻找的功能,它干净且可读。
这是有效的,因为std::array<int, 5>
命名template <typename... T>
期望的类型,并且它包含定义std::array
所需的std::SIZE_T
信息。
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- 如何使用可变参数模板强制转换每个变体类型
- 关于如何在具有单个参数的变体构造中选择替代方案?
- 调用参数排列不变函数 f(i++, i++)
- 参数归纳与标准::变体
- 模板化回调参数的逆变,如 C# 中的逆变
- 如何在没有参数包的情况下编写变差函数
- 通过具有嵌套类的工厂类获取多个变异类模板参数包
- 获取模板参数的成员变量值列表
- 保留短 lambda 用作函数的中间参数,使用 clang 格式保持不变
- 如何定义变体<x,y,z>提取模板参数的子类型
- 正确对齐内存模板,参数顺序不变
- 递归中不同参数类型的变元模板函数
- 通过函数指针传递给变差函数的参数会更改其值
- 提升预定义为带有参数的全局 lambda 的变体访问者
- 使用可变参数模板参数提升变体访问者
- boost ::变体 - 为什么模板参数比const字符串参数具有更高的优先级
- 将变参数包中的值加载到临时数组中
- 使用额外参数提升变体访客
- 从变长参数列表中提取std::string