34

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?

M.M
  • 138,810
  • 21
  • 208
  • 365
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 1
    **Related:** http://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 Dec 21 '11 at 17:23
  • 2
    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 Dec 21 '11 at 17:26
  • @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 Dec 21 '11 at 17:29
  • 1
    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 Dec 21 '11 at 17:34
  • @Steve: Heh, okay. That makes sense then. – Lightness Races in Orbit Dec 21 '11 at 17:36
  • 1
    C99 VLAs have different behaviour to GCC's arrays . C99 didn't adopt GCC arrays. – M.M Aug 28 '14 at 02:12

4 Answers4

18

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

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
  • Revisiting this, what I quoted is normative and seems to heavily imply to me that C++ provides `the facilities provided by C` – Lightness Races in Orbit Jan 08 '13 at 16:50
  • @LightnessRacesinOrbit by what specifically in that sentence do you infer that? The statement "A is based on B" does not mean that every feature of B is present in A. For example a movie "based on a true story" does not contain every feature of the true story it's based on. And if you write a proposal "based on another proposal", it doesn't necessarily mean that any feature the other proposal has is in your proposal aswell. Unless given a formal definition of the "based on" relation, I will go by the standard everydays meaning. – Johannes Schaub - litb Jun 27 '16 at 16:02
  • 1
    It's not the "based on" sentence. It's the sentence after it. _"In addition to the facilities provided by C, C++ provides..."_ This means that C++ provides at minimum the facilities provided by C. – Lightness Races in Orbit Jun 27 '16 at 16:05
  • I read that "facility" can mean "something that permits the easier performance of an action". I assume that the guy who wrote that sentence thinks that VLAs do not simplify the performance of creating a variable length array. – Johannes Schaub - litb Jun 27 '16 at 16:07
  • Having support for VLAs in the language simplifies the performance of creating a variable length array. – Lightness Races in Orbit Jun 27 '16 at 16:13
  • 1
    @light i was arguing for fun :) yes I agree it would imply that C++ has VLAs. But I don't consider it a defect (if anything it is a contradiction with later text that make VLAs illformed, rather than allowing VLAs). Introductional paragraphs are imprecise on purpose (IIRC that was also mentioned on the "how to report an issue" page of the std-c++ usenet group, as a word of caution). – Johannes Schaub - litb Jun 28 '16 at 14:24
  • 1
    _"(if anything it is a contradiction with later text that make VLAs illformed, rather than allowing VLAs)"_ Exactly the point of this question :) – Lightness Races in Orbit Jun 28 '16 at 15:54
14

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • 6
    I don't think it's even an "override" -- C features aren't in C++ by inheritance, they're in there by composition-and-exposure. Any C feature not explicitly mentioned in the C++ standard is absent, exactly as if C++ "inherited" nothing from C but just coincidentally contains similar text. But the authors clearly are motivated to provide C features where possible and where they don't think they're stupid. – Steve Jessop Dec 21 '11 at 17:30
  • Does `int x = foo(); int n[x];` count? That's working for me on g++4.6. Surely x isn't a *constant expression* here? – Aaron McDaid Dec 21 '11 at 18:01
  • 1
    @Aaron McDaid If you didn't compile with `-pedantic` (or is it `-ansi`?) you probably got the g++ extension. – Mark B Dec 21 '11 at 18:03
  • @MarkB, `-pedantic` gives me "template.cpp:7:12: warning: ISO C++ forbids variable length array ‘n’ [-Wvla]" I had -Wall and -Wextra previously. – Aaron McDaid Dec 21 '11 at 18:07
  • Thanks @MarkB, I'll modify my answer accordingly. – Aaron McDaid Dec 21 '11 at 18:07
  • @AaronMcDaid: There you go. :) VLAs should be disabled by default IMO >. – Lightness Races in Orbit Dec 21 '11 at 18:07
  • Yeah :-) There are obvious alternatives to VLA available in C++. Also, I want an option that *really* turns on all the warnings. Is `-Wall -Wextra -pedantic` sufficient? At least until I decide which warnings to turn off again. – Aaron McDaid Dec 21 '11 at 18:13
  • To really get a lot of warnings, you should use `-Wall -Wextra -std=c++98 -pedantic` or `-Wall -Wextra -std=c++0x -pedantic` (so, "yes"). – Lightness Races in Orbit Dec 21 '11 at 18:14
  • 3
    From GCC's point of view it's not really a question of "all the warnings" so much as "what language would you like me to compile". If you don't say `-pedantic` then you are not compiling C++, you're compiling C++-with-extensions. If you say `std=gnu++0x` you get extensions *and* also language changes that contradict the standard rather than validly extending it. I assume that compiler-writers don't make this the default for the same reason that they think it's a good idea to extend the language in the first place (a combination of hubris and whingeing customers). – Steve Jessop Dec 21 '11 at 18:28
  • @SteveJessop: More win from you. – Lightness Races in Orbit Dec 21 '11 at 18:30
  • And also note that `gnu++98` is the default, so if you don't say anything at all then you don't have a conforming C++ compiler. – Steve Jessop Dec 21 '11 at 18:34
  • what's "validly extending" mean? – Johannes Schaub - litb Dec 24 '11 at 13:11
  • @Johannes - It means that any valid C++ program compiles... And some invalid ones do, too. – Nemo Aug 15 '12 at 00:28
7

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
Happy Green Kid Naps
  • 1,611
  • 11
  • 18
  • @DrumM -- What are you talking about? Were you thinking of a different answer (or question?!) when you added your comment? What is wrong with the answer that would get you to downvote? – Happy Green Kid Naps Jul 08 '20 at 19:18
  • Your answer reflects to a `constexpr` which is not possible in 99% of the cases, people use the variable length array when the size is not a `const`, e.g. when it's an input variable. So even if you declare a const from an input variable, it's still not a compile time constant. The `const size_t size = 5;` is just a useless extra line... – jaques-sam Jul 10 '20 at 11:47
  • First what did you mean by " _This does not solve the warning_ "? Second, I was explaining a subtle difference between C and C++ for syntactically identical lines in the two languages. By bringing in `constexpr` and seeing the `const int` declaration as a _useless extra line_ , you are clearly missing the point. – Happy Green Kid Naps Jul 10 '20 at 15:01
4

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

Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
  • (a) GCC does have VLAs as an extension, too. (b) I've seen failures to detect when the array dimension is variable, in the past. – Lightness Races in Orbit Dec 21 '11 at 18:07
  • 3
    That's the g++ extension. If you compile with `-pedantic` it warns that it's using the compiler extension and is *not* ISO C++. – Mark B Dec 21 '11 at 18:07