使用g++编译C++源代码时的VLA

VLAs when compiling a C++ source with g++

本文关键字:VLA 源代码 C++ g++ 编译 使用      更新时间:2023-10-16

我知道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模式中发出警告,因此得到了正确更新。