[英]C++ templated, statically allocated array
I'm trying to do something like this for a project... 我正在为一个项目做这样的事情...
template <class N> class MyClass { float properties[N]; };
Is there anyway to accomplish this in C++? 无论如何,用C ++可以做到这一点吗?
What you need is called value template parameter: 您需要的称为值模板参数:
template <size_t N> class MyClass { float properties[N]; };
//^^^^^^ note this
Now you can instantiate this class template, passing any non-negative integral value as template argument. 现在,您可以实例化此类模板,并将任何非负整数值作为模板参数传递。 For example,
例如,
MyClass<10> c1; //N=10
MyClass<100> c1; //N=100
You can pass const expression also as: 您还可以将const表达式传递为:
const size_t size = 200;
MyClass<size> c2; //N=200
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.