Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 不带constexpr size的内置数组初始化_C++_Arrays_C++11_Declaration_Constexpr - Fatal编程技术网

C++ 不带constexpr size的内置数组初始化

C++ 不带constexpr size的内置数组初始化,c++,arrays,c++11,declaration,constexpr,C++,Arrays,C++11,Declaration,Constexpr,我很难弄清楚,我如何能够在不使用constexpr维度的情况下声明内置数组。也就是说,下面的块非常适合我。我知道内置数组有固定的大小,必须在编译时通过constexpr大小定义来指定 int size; int arr[size]; 不一定是通过constexpr,这是从C++11引入的 您可以通过一个简单的数字来指定大小 int arr[12]; 还是常数 int const size = 7; int arr[size]; 或者是C风格的宏 #DEFINE size 5 int

我很难弄清楚,我如何能够在不使用
constexpr
维度的情况下声明内置数组。也就是说,下面的块非常适合我。我知道内置数组有固定的大小,必须在编译时通过
constexpr
大小定义来指定

int size;
int arr[size];

不一定是通过
constexpr
,这是从C++11引入的

您可以通过一个简单的数字来指定大小

int arr[12];
还是常数

int const size = 7;
int arr[size];
或者是C风格的宏

#DEFINE  size  5

int arr[size];
但你是对的:下面的代码

int size = 11;
int arr[size];

不是正确的C++。< /P>

但在某些版本的C中是合法的(只有在C99中,如果我记得很好),C++编译器通常也是C编译器。因此,他们通常是宽容的


您可以尝试使用特定选项进行编译,以对语言进行严格检查;例如,使用g++和clang++,我使用
-ansi-pedantic

也可以通过以下方式初始化大小为
n
的数组:

int n = 123;
int* a;
a = new int[n];

非常感谢,从现在起我将使用-pedantic,而-ansi由于某些原因不起作用