[英]std::array with unspecialized template class
Is it possible to pass somehow unspecialized template class as a template parameter to std::array? 是否可以通过某种方式将非专业的模板类作为模板参数传递给std :: array? Something similar to that:
类似于以下内容:
template <class T>
class Field{
};
std::array<Field> a;
Or I have to define some BaseField, and sublass it as a IntField, StringField, FloatField.... ? 或者我必须定义一些BaseField,并将其细分为IntField,StringField,FloatField ....?
You can write a template that takes a template as a parameter (aka, a template template parameter). 您可以编写一个以模板为参数(也称为模板模板参数)的模板。 The template has to be written specifically to take a template as a parameter to allow it though, and
std::array
isn't specified to do that. 模板必须专门写成将模板作为参数来允许它,但是没有指定
std::array
来执行此操作。
You can get pretty close with C++11, using an alias template: 您可以使用别名模板与C ++ 11紧密联系:
template <typename T>
struct Field {};
template <typename T>
using FieldArray5 = std::array<Field<T>,5>;
int main() {
FieldArray5<int> a0;
}
But bear in mind that std::array
needs a template argument for the size too. 但是请记住,
std::array
需要模板参数来指定大小。
If you want an std::array
to hold different types, it can only be done through polymorphism. 如果要让
std::array
保留不同的类型,则只能通过多态来完成。 You can also use boost::any
that hides that polymorphism from you but still implements it. 您也可以使用
boost::any
将多态隐藏起来但仍然实现的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.