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?

6
  • 1
    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) Commented Dec 21, 2011 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" ;-) Commented Dec 21, 2011 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. Commented Dec 21, 2011 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. Commented Dec 21, 2011 at 17:34
  • 1
    C99 VLAs have different behaviour to GCC's arrays . C99 didn't adopt GCC arrays. Commented Aug 28, 2014 at 2:12

4 Answers 4

19

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

Sign up to request clarification or add additional context in comments.

7 Comments

Revisiting this, what I quoted is normative and seems to heavily imply to me that C++ provides the facilities provided by C
@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.
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.
@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).
"(if anything it is a contradiction with later text that make VLAs illformed, rather than allowing VLAs)" Exactly the point of this question :)
|
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.

12 Comments

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.
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 If you didn't compile with -pedantic (or is it -ansi?) you probably got the g++ extension.
@MarkB, -pedantic gives me "template.cpp:7:12: warning: ISO C++ forbids variable length array ‘n’ [-Wvla]" I had -Wall and -Wextra previously.
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).
|
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

3 Comments

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

2 Comments

(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.
That's the g++ extension. If you compile with -pedantic it warns that it's using the compiler extension and is not ISO C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.