使用g ++编译C ++源时的VLA [英] VLAs when compiling a C++ source with g++

查看:146
本文介绍了使用g ++编译C ++源时的VLA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道VLA是C99的一部分,但不是C ++ 0x。我的问题是为什么和如何 g ++ 将编译代码与VLAs。我在下面写了一个测试用例:



test.c

  #includestdio.h

int main(int argc,char * argv []){
int i;
int array [argc];

for(i = 0; i array [i] = i;

for(i = 0; i printf(%d,i)
puts();

return 0;
}

我也将它复制到一个名为 cpp 。以下所有编译:

  $ gcc test.c -o test // expected 
$ g ++ test.cpp - o测试//不期望
$ g ++ test.cpp -o test -x c ++ //真的不期望
$ g ++ test.cpp -o test -std = c ++ 11 //真的不期望

我已阅读SO问题 C ++中的可变长度数组? g ++和gcc之间有什么区别?但是我找不到为什么 g ++ 会做这个或者怎么做的答案是的。这似乎是非常不可预测的行为,因为标准没有指定VLA。 g ++ 将此识别为C代码,并使用 cc1 编译?有没有办法强制 g ++ 坚持标准?



此外, :



test2.cpp

  #includestdio.h
#include< vector>

int main(int argc,char * argv []){
int i;
int array [argc];

for(i = 0; i array [i] = i;

for(i = 0; i printf(%d,i)
puts();

std :: vector< decltype(array)>温度

return 0;
}

然后无法编译:

  $ g ++ test2.cpp -o test2 -std = c ++ 11 
test2.cpp:在函数'int main(int,char **)' :
test2.cpp:16:32:error:'int [((sizetype)((ssizetype)argc)+ -1))+ 1)]'是一个可变修改类型
std :: vector< decltype(array)>温度
^
test2.cpp:16:32:error:试图实例化'template< class> class std :: allocator'
test2.cpp:16:32:error:模板参数2无效
test2.cpp:16:38:error: $ b std :: vector< decltype(array)>温度

这个错误似乎意味着我仍然可以声明VLA,我只是不能作为 decaltype 的参数。这使得一切看起来很陌生。我可以理解明显的答案,这是错误的,所以只是不要这样做或者,如果你使用C ++,使用std :: vector,但这似乎是一个结束游戏类型的答案,



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



系统信息,如果有区别:

  $ lsb_release -a 
LSB模块。
经销商ID:Ubuntu
描述:Ubuntu 14.04.1 LTS
发行版:14.04
代号:trusty

$ g ++ -v
内置规格。
COLLECT_GCC = g ++
COLLECT_LTO_WRAPPER = / usr / lib / gcc / x86_64-linux-gnu / 4.8 / lto-wrapper
目标:x86_64-linux-gnu
配置为:。 ./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 --unhout-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
线程模型:posix
gcc版本4.8.2(Ubuntu 4.8.2-19ubuntu1)


解决方案

VLAs是C ++模式下的gcc(和clang)编译器扩展,记录这里。默认情况下会启用这些扩展。



如果使用 -pedantic 编译,您应该会看到警告。



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



clang在C ++ 14模式下仍然警告,它应该。



编辑: gcc 5.1在C ++ 14模式警告, $ b

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;
}

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

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;
}

Which then fails to compile:

$ 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;

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.

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

System information in case that is the difference:

$ 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) 

解决方案

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

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

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 and might appear in C++17.

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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆