[英]Why does S have to be const in this code?
Why doesn't the below code compile?为什么下面的代码不能编译? It says that
S
has to be const
as one of the major errors.它说
S
必须是const
作为主要错误之一。
template <int S>
class Array
{
int size;
int items [S];
public:
Array(void) :size(S){}
};
void main()
{
int S= 30;
Array <5+S> array;
}
Nontype template parameters must be constexpr
, ie, they have to be known at compile time.非类型模板参数必须是
constexpr
,即它们必须在编译时已知。 Hence, S
must be declared as constexpr int
.因此,
S
必须声明为constexpr int
。
What would sizeof(array)
return? sizeof(array)
返回什么? Remember, sizeof
is a compile time operator. 请记住,
sizeof
是一个编译sizeof
算符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.