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

VLAs when compiling a C++ source with g++(使用 g++ 编译 C++ 源代码时的 VLA)
本文介绍了使用 g++ 编译 C++ 源代码时的 VLA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解 VLA 是 C99 的一部分,但不是 C++0x.那么我的问题是 g++ 为什么以及如何使用 VLA 编译代码.我在下面写了一个测试用例:

I understand that VLAs are part of C99 but not C++0x. My question is then why and how g++ would compile code with VLAs. I wrote a test case below:

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 的文件中.以下全部编译:

I also copied it into a file called test.cpp. The following all compile:

$ 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 有什么区别? 但我找不到回答为什么 g++ 会这样做或如何做.这似乎是极其不可预测的行为,因为标准没有指定 VLA.g++ 是否将其识别为 C 代码并使用 cc1 进行编译?有没有办法强制g++遵守标准?

I have read the SO questions Variable length arrays in C++? and What is the difference between g++ and gcc? but I cannot find an answer as to why g++ would do this or how it does. This seems like extremely unpredictable behavior as the standard specifies no VLAs. Does g++ recognize this as C code and compile with cc1? Is there a way to force g++ to stick to the standard?

另外,我可以用以下方法打破它:

Additionally, I can break it with the following:

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"的明显答案,但这似乎是一种最终游戏类型的答案并且没有还没有找到问题的根源.

This error appears to mean that I am still allowed to declare VLAs, I just can't have them as an argument to decaltype. This makes everything seem much stranger. I can understand the obvious answer of, "It's wrong, so just don't do it" or, "if you're using C++, use std::vector" but that seems like an end-game type of answer and doesn't quite get to the root of the issue.

我无法在 Visual Studios 上进行编译以进行比较,因为我目前无权访问它.

I cannot compile on Visual Studios to compare as I do not currently have access to it.

系统信息以防万一:

$ 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)编译器扩展,记录在 这里.默认情况下启用这些扩展.

VLAs are a gcc (and clang) compiler extension in C++ mode, documented here. These extensions are enabled by default.

如果您使用 -pedantic 进行编译,您应该会看到一个警告.

You should see a warning if you compile with -pedantic.

有趣的是,你不会使用 gcc4.9 和 -std=c++14-std=c++1y.当他们解释 here 时,他们删除了 -Wvla C++14 模式中的警告,因为 VLA 曾经在标准草案中.他们实际上并没有在 C++14 的最终版本中实现它.

Funnily, you will not with gcc4.9 and -std=c++14 or -std=c++1y. As they explain here, they removed the -Wvla warning in C++14 mode because VLAs were once in the draft for the standard. They did not actually make it in the final version of C++14.

clang 仍然在 C++14 模式下发出警告,这是应该的.

clang still warns in C++14 mode, as it should.

gcc 5.1 在 C++14 模式下也会发出警告,因此已正确更新.

gcc 5.1 warns in C++14 mode too, so that got updated correctly.

这篇关于使用 g++ 编译 C++ 源代码时的 VLA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)