Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
593 views
in Technique[技术] by (71.8m points)

c++11 - Are variable length arrays an extension in Clang too?

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?

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;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

So as you say, a program such 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;
}

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

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

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...