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

``````  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;
};
``````

``````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;
}
``````

``````#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;
}
``````

0 条评论