我知道 VLA 是 C99 的一部分,但不是 C++0x 的一部分。我的问题是为什么以及如何 g++
使用 VLA 编译代码。我在下面写了一个测试用例:
test.c
#include "stdio.h"
int main(int argc, char *argv[]) {
int i;
int array[argc];
for(i = 0; i < argc; i++)
array[i] = i;
for (i = 0; i < argc; i++)
printf("%d ", i);
puts("");
return 0;
}
我还将其复制到名为 test.cpp
的文件中。以下全部编译:
$ gcc test.c -o test // expected
$ g++ test.cpp -o test // not expected
$ g++ test.cpp -o test -x c++ // really not expected
$ g++ test.cpp -o test -std=c++11 // really not expected
我已阅读SO问题Variable length arrays in C++?和 What is the difference between g++ and gcc?但我找不到关于为什么 g++
会这样做或者它是如何做的答案。这似乎是极其不可预测的行为,因为标准没有指定 VLA。 g++
是否将其识别为 C 代码并使用 cc1
进行编译?有没有办法强制 g++
遵守标准?
此外,我可以通过以下方式打破它:
test2.cpp
#include "stdio.h"
#include <vector>
int main(int argc, char *argv[]) {
int i;
int array[argc];
for(i = 0; i < argc; i++)
array[i] = i;
for (i = 0; i < argc; i++)
printf("%d ", i);
puts("");
std::vector<decltype(array)> temp;
return 0;
}
编译失败:
$ g++ test2.cpp -o test2 -std=c++11
test2.cpp: In function ‘int main(int, char**)’:
test2.cpp:16:32: error: ‘int [(((sizetype)(((ssizetype)argc) + -1)) + 1)]’ is a variably modified type
std::vector<decltype(array)> temp;
^
test2.cpp:16:32: error: trying to instantiate ‘template<class> class std::allocator’
test2.cpp:16:32: error: template argument 2 is invalid
test2.cpp:16:38: error: invalid type in declaration before ‘;’ token
std::vector<decltype(array)> temp;
此错误似乎意味着我仍然可以声明 VLA,只是不能将它们作为 decaltype
的参数。这让一切都显得更加陌生。我可以理解明显的答案,“这是错误的,所以不要这样做”,或者“如果你使用 C++,请使用 std::vector”,但这似乎是一种最终游戏类型的答案,并且没有还没有完全找到问题的根源。
我无法在 Visual Studio 上进行编译进行比较,因为我当前无法访问它。
系统信息(如有差异):
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 14.04.1 LTS
Release: 14.04
Codename: trusty
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.8/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.8.2-19ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.8/README.Bugs --enable-languages=c,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.8 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.8 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --disable-libmudflap --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-4.8-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-4.8-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
最佳答案
VLA 是 C++ 模式下的 gcc(和 clang)编译器扩展,记录为 here 。默认情况下启用这些扩展。
如果使用 -pedantic
进行编译,您应该会看到一条警告。
有趣的是,您不会使用 gcc4.9 和 -std=c++14
或 -std=c++1y
。正如他们所解释的 here ,他们删除了 C++14 模式下的 -Wvla
警告,因为 VLA 曾经在标准草案中。他们实际上并没有在 C++14 的最终版本中实现这一点。
clang 在 C++14 模式下仍然会发出警告,正如它应该的那样。
编辑: gcc 5.1 也在 C++14 模式下发出警告,因此已正确更新。
关于c++ - 使用 g++ 编译 C++ 源代码时的 VLA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28421275/