【发布时间】:2016-08-01 15:35:45
【问题描述】:
假设我有几个像这样的容器类:
template<typename T> class Container
{
/* ... */
};
template<typename T, size_t> class Array : public Container<T>
{
/* Fixed-sized Container */
};
template<typename T> class Vector : public Container<T>
{
/* Variable-sized Container */
};
我有一个类接受其中一个作为模板参数:
template<typename T, template<typename> class U, size_t M> class Polygon
{
U<T> Vertices; // Problem, what if user passes Array (it needs 2 parameters)
U<T, M> Vertices; // Problem, what if the user wants to use a variable-sized container (it needs only 1 parameter)
};
我的问题是,我能否以某种方式(可能通过棘手的模板参数魔术)使消费类接受任何类型的容器(固定或可变大小,即使具有不同的模板签名)?
关于模板签名的唯一保证是,如果它是一个固定大小的容器,它将有两个参数<Type, Size>,如果它是一个可变大小的容器<Type>
【问题讨论】:
标签: c++ templates containers