根据C++标准,数组的大小必须大于零并且编译时间常数。如果数组是VLA,那么它必须具有自动存储持续时间,即数组必须是本地的。换句话说:为什么C++中的可变大小数组(VLA)需要自动(自动)存储类?
#include<iostream>
int size = 10;
constexpr int Size = 10;
int ar[size]; /* error as size is not a compile time constant and ar does not have auto storage class. */
int Ar[Size]; // fine, no error as Size is a compile time constant.
int main()
{
int arr[size]; // fine as arr has auto storage class.
return 0;
}
所以,我的问题是 - 为什么我们不能有C++中的VLA具有静态存储时间?
C++没有VLA。有编译器特定的扩展允许C风格的VLA,但它们是非标准的。您应该指定使用哪个编译器和哪个C++版本。 – hyde
*为什么我们不能在C++中使用具有静态存储持续时间的VLA * - 在C++中没有VLA。使用'std :: vector'。 – PaulMcKenzie
@hyde用g ++编译为Code :: Blocks IDE(遵循ISO C++ 11标准)。 –