34
votes

No, wait, bear with me...

VLAs were always a GCC extension, but they were adopted by C99:

[C99: 6.7.5.2/4]: If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope; such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

C99 is also known as ISO/IEC 9899:1999.

Now:

[C++11: 1.1/2]: C++ is a general purpose programming language based on the C programming language as specified in ISO/IEC 9899:1999 (hereinafter referred to as the C standard). In addition to the facilities provided by C, C++ provides additional data types, classes, templates, exceptions, namespaces, operator overloading, function name overloading, references, free store management operators, and additional library facilities.

So shouldn't C++11 have VLAs too?

4
Related: stackoverflow.com/questions/1887097/variable-length-arrays-in-c (though my question is really about where it's technically stated that the feature is not inherited from C99 in the first place; the other is about asking whether the committee could explicitly make it so)Lightness Races in Orbit
Anyway, "based on the C programming language" is informative text, I think. "In addition to the facilities provided by C" means "in addition to some facilities provided by C and that we incorporated into C++", not "oh, and if there's anything from C that we forget to mention in the following text, then that's in C++ too" ;-)Steve Jessop
@Steve: OK. Maybe the issue I've had then is in assuming that the C language, as well as the C standard library, is by default "inherited" from C99. Perhaps it's just the library, and the language is merely cited as a basis for design.Lightness Races in Orbit
Well, each library function in turn that's taken from C is listed in the C++ standard, with a reference to the C99 standard saying, "this function is the same as over there", and in a few cases "... with the following difference". So I agree, there is no wholesale inheritance, just a cherry-picking exercise that picks 99% of the cherries.Steve Jessop
C99 VLAs have different behaviour to GCC's arrays . C99 didn't adopt GCC arrays.M.M

4 Answers

17
votes

That leeway wording doesn't mean that any and everything in C99 is in C++11. What you quoted is just introductory text.

14
votes

This C99 feature is effectively overridden by C++'s own semantics, as can be any otherwise "inherited" feature:

[C++11: 8.3.4/1]: In a declaration T D where D has the form

D1 [ constant-expressionopt ] attribute-specifier-seqopt

[..]

This is the only array declaration syntax we're given in C++.

Note that no mention of this difference is given in the "compatibility with C" clause C.1.

7
votes

The definition of constant-expression is different for the two languages.

const size_t size = 5;
int array[size]; // array in C++, VLA in C
4
votes

This compiles for me: (g++ 4.6 with -std=c++0x). But it doesn't compile with -pedantic (thanks @MarkB). Instead it warns that "template.cpp:7:12: warning: ISO C++ forbids variable length array ‘n’ [-Wvla]"

int main(int argc, char ** argv) {
    int n[argc];
}

So the size of n can not be known at compile time by the compiler. Is this a GNU extension to C++? This does appear to be a GNU extension, and that VLAs are not an official part of C++11.

(Of course, I'm just playing with a compiler. So take this with a pinch of salt.)