使用g++编译C++源代码时的VLA
VLAs when compiling a C++ source with g++
我知道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问题C++中的可变长度数组?g++和gcc之间有什么区别?但我找不到CCD_ 4为什么会这样做或如何这样做的答案。这似乎是一种极不可预测的行为,因为标准中没有指定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)编译器扩展,本文对此进行了说明。默认情况下会启用这些扩展。
如果使用-pedantic
进行编译,则应该看到一条警告。
有趣的是,您不会使用gcc4.9和-std=c++14
或-std=c++1y
。正如他们在这里解释的那样,他们在C++14模式中删除了-Wvla
警告,因为VLA曾经在标准草案中。他们实际上并没有在C++14的最终版本中实现。
clang仍然在C++14模式下发出警告,这是应该的。
编辑:gcc 5.1也在C++14模式中发出警告,因此得到了正确更新。
- 在Linux中哪里可以找到互斥、未来等的源代码
- 如果编译的源代码是特定于它编译的硬件的,我们如何分发它
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 难以理解某些人解决IOI问题的源代码
- 使用g++编译hdf5源代码
- C++中的VLA,扩展名为std=C++11
- 如何使用C++读取另一个程序中的源代码输出
- Poco FindMySql.cmake 无法找到 MySQL 源代码
- 更改命令行 qt5 源代码构建配置的正确/快速方法
- C++出现控制台错误.我无法识别源代码的问题
- Qt Quick,如何更改 Ui 源代码?
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- 从macOS Catalina上的源代码编译LLVM(如何解决未找到"stdio.h")
- 如何使用QT创建器获取网页源代码?
- 从源代码构建 gRPC C++
- 如何使用我构建的库,而不会从源代码出错,但不为我自己的项目编译?
- 如何在使用 LLVM 编译时获取部分源代码的二进制代码
- 如何在源代码中包含外部库的头文件?
- 为什么即使此代码使用 int,仍被视为 VLA?
- 使用g++编译C++源代码时的VLA