如何拥有具有类型和大小的可变参数模板

How to have variadic templates with a type and a size?

本文关键字:变参 参数 类型 何拥有 拥有      更新时间:2023-10-16

只是为了好玩,我试图重载一个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信息。