Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.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

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++;获取常量的函数中的可变大小堆栈数组 我现在正在学习C++,我正在读一本有效的C++(Scott Meyers)书。 在这本书中,有一个关于常量变量的项目,我尝试使用它们。 我注意到一些非常有趣的事情,如果它在C++中出现bug,我想知道什么: (我正在使用C++98标准)_C++_Arrays_Constants - Fatal编程技术网

c++;获取常量的函数中的可变大小堆栈数组 我现在正在学习C++,我正在读一本有效的C++(Scott Meyers)书。 在这本书中,有一个关于常量变量的项目,我尝试使用它们。 我注意到一些非常有趣的事情,如果它在C++中出现bug,我想知道什么: (我正在使用C++98标准)

c++;获取常量的函数中的可变大小堆栈数组 我现在正在学习C++,我正在读一本有效的C++(Scott Meyers)书。 在这本书中,有一个关于常量变量的项目,我尝试使用它们。 我注意到一些非常有趣的事情,如果它在C++中出现bug,我想知道什么: (我正在使用C++98标准),c++,arrays,constants,C++,Arrays,Constants,此函数将完全按照我的要求编译和工作(在堆栈上创建大小为“I”的int数组)。当我从“I”中删除“const”时,它将不会编译。 我在gcc和叮当声上试了一下 编辑: 链接到首先,编译器会忽略函数签名中的常量。因此以下两个是等效的: Test(const int i) {} Test(int i) {} 第二,无论编译与否,这都不是有效的C++: int arr[i] = {0}; 它无效,因为i不是编译时常量,即编译时必须知道i的值 试试看首先,编译器会忽略函数签名中的常量。因此以下两个是等

此函数将完全按照我的要求编译和工作(在堆栈上创建大小为“I”的int数组)。当我从“I”中删除“const”时,它将不会编译。 我在gcc和叮当声上试了一下

编辑:
链接到

首先,编译器会忽略函数签名中的常量。因此以下两个是等效的:

Test(const int i) {}
Test(int i) {}

第二,无论编译与否,这都不是有效的C++:

int arr[i] = {0};
它无效,因为
i
不是编译时常量,即编译时必须知道
i
的值


试试看

首先,编译器会忽略函数签名中的常量。因此以下两个是等效的:

Test(const int i) {}
Test(int i) {}

第二,无论编译与否,这都不是有效的C++:

int arr[i] = {0};
它无效,因为
i
不是编译时常量,即编译时必须知道
i
的值


试试看

将来要抓住这种错误,你想要的g++和clang++的编译器标志都是
-pedantic
。记住要指定你的语言标准,否则你不知道会得到什么

$ g++ -std=c++98 -pedantic c++-vla.cpp -o c++-vla
c++-vla.cpp: In function ‘void f(size_t)’:
c++-vla.cpp:3:30: warning: ISO C++ forbids variable length array ‘g’ [-Wvla]
    3 | void f(const size_t x) { int g[x]; }
      |                              ^

$ clang++ -std=c++98 -pedantic c++-vla.cpp -o c++-vla
c++-vla.cpp:3:31: warning: variable length arrays are a C99 feature [-Wvla-extension]
void f(const size_t x) { int g[x]; }
                              ^
1 warning generated.

为了在将来发现这种错误,您希望g++和clang++的编译器标志都是
-pedantic
。请始终记住指定您的语言标准,否则您不知道会得到什么

$ g++ -std=c++98 -pedantic c++-vla.cpp -o c++-vla
c++-vla.cpp: In function ‘void f(size_t)’:
c++-vla.cpp:3:30: warning: ISO C++ forbids variable length array ‘g’ [-Wvla]
    3 | void f(const size_t x) { int g[x]; }
      |                              ^

$ clang++ -std=c++98 -pedantic c++-vla.cpp -o c++-vla
c++-vla.cpp:3:31: warning: variable length arrays are a C99 feature [-Wvla-extension]
void f(const size_t x) { int g[x]; }
                              ^
1 warning generated.

我认为这不是标准,它可能是GCC/CLAN扩展。在C++中,数组大小必须在编译时知道。它们会为同一个东西得到一个错误。一些编译器支持C++中的C可变长度数组。但是使用它不是一个好主意。它与其他C++特性(如模板和对象创建和DESTR)交互很差。Untuff.ZhanLyx,但是如果编译器支持VLA,它也应该工作在没有代码> const 。我不认为这是标准的,它可能是Gcc/CLAN扩展。在C++中,数组大小必须在编译时知道。它们会为同一个东西得到一个错误。一些编译器支持C++中的C可变长度数组。这不是一个好主意。但是,它与其他的C++特性(如模板和对象创建和销毁)交互很差。@如果编译器支持VLA,它也应该工作在没有<代码> const >的情况下。我认为函数签名中的<代码> const < /COD>防止在函数内分配变量。是的,但是对编译器来说,它不做DIF。引用是否为常量(对于值类型)。在决定是否允许将其用作数组大小时,这没有什么区别,但在其他方面会有所不同。我在compiler compiler Explorer上试用了它-这是编译好的,可以工作,这是因为GCC自己的扩展使它可以工作。用
-pedantic
编译,然后看看会发生什么。另外,请阅读这篇文章以了解bigger回答为什么GCC允许这样做:我认为函数签名中的
const
会阻止在函数内部分配变量。是的,但对于编译器来说,它是否为
const
没有区别(对于值类型)。在决定是否允许将其用作数组大小时,这没有什么区别,但在其他方面会有所不同。我在compiler compiler Explorer上试用了它-这是编译好的,可以工作,这是因为GCC自己的扩展使它可以工作。用
-pedantic
编译,然后看看会发生什么。另外,请阅读这篇文章以了解bigger回答GCC为什么允许这样做: