是可变长度数组在Clang中的扩展吗? [英] Are variable length arrays an extension in Clang too?

查看:635
本文介绍了是可变长度数组在Clang中的扩展吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,VLA不是C ++ 11的一部分,我已经看到了GCC的这一滑。这是我切换到Clang的部分原因。但现在我看到它ang。我使用clang 3.2(最后一个),我正在使用
编译 -pedantic和-std = c ++ 11

I understand that VLAs are not part of C++11 and I have seen this slip by GCC. It is part of the reason I switched to Clang. But now I am seeing it Clang too. I am using clang 3.2 (one behind the latest) and I am compiling with -pedantic and -std=c++11

我期望我的测试不编译,但它编译和运行。

I expect my test to NOT compile yet it compiles and runs.

int myArray[ func_returning_random_int_definitely_not_constexpr( ) ];

这是一个编译器错误还是缺少一些东西?

Is this a compiler bug or an I missing something?

响应此处的注释是random_int_function()

In response to the comment here is the random_int_function()

#include <random>
int random_int_function(int i) 
{
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,100);

    int random_int = distribution(generator);  

    return i + random_int;
}


推荐答案

支持在clang 3.2 / 3.3中与
相反的C ++ 11标准(第8.3.4 / 1节)。

Yes, variable length arrays are supported in clang 3.2/3.3 contrary to the C++11 Standard (§ 8.3.4/1).

as:

#include <random>

int random_int_function(int i) 
{
    std::default_random_engine generator;
    std::uniform_int_distribution<int> distribution(1,100);

    int random_int = distribution(generator);  

    return i + random_int;
}

int main() {
    int myArray[ random_int_function( 0 ) ];
    (void)myArray;
    return 0;
}

编译和运行。但是,使用选项 -pedantic; -std = c + + 11 ,表示
你说你通过,clang 3.2 / 3,3诊断:

compiles and runs. However, with the options -pedantic; -std=c++11 that you say you passed, clang 3.2/3,3 diagnoses:


警告:可变长度数组是一个C99特性[-Wvla]

warning: variable length arrays are a C99 feature [-Wvla]

行为与gcc /4.8.1),它更加强调警告:

The behaviour matches that of gcc (4.7.2/4.8.1), which warns more emphatically:


警告:ISO C ++禁止可变长度数组'myArray'[-Wvla] p>

warning: ISO C++ forbids variable length array ‘myArray’ [-Wvla]

为了使诊断成为错误,对于任一编译器,传递 -Werror = vla

To make the diagnostic be an error, for either compiler, pass -Werror=vla.

这篇关于是可变长度数组在Clang中的扩展吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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