1

I have a question closely related to this one, the difference being that my code sample does not use new

I repeat here the question:

On page 11 of A Tour of C++ (second edition) is written

The size of an array must be a constant expression.

And the nearby examples indicate he means C-style arrays, not std::array.

So the following:

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

should not compile, yet does (GCC 9.3.0, g++ -std=c++17 -Wall) without warnings.

(This question has a partial answer in Are variable length arrays there in c++? I did not find it because I did not know/forgot about the terminology variable length array. Below I complete this with an answer to my own question that gives a way to force GCC to comply with current ISO.)

Arnaud
  • 247
  • 1
  • 8

1 Answers1

2

OK, that's a specificity of the compiler I use, GCC: it implements by defaults VLA (Variable Length Arrays) even though this is not part of the C++17 standard. In fact there is a mention of this in Barry's answer (accepted answer) to the related question, to be found in his footnote.

With the option -pedantic-errors the compilation does fail, as expected.

Arnaud
  • 247
  • 1
  • 8
  • 2
    This doesn't address the question, but the C++ standard only requires a compiler to refuse to compile code in one case: when it encounters a `#error` directive. In all other cases, a conforming compiler must issue a diagnostic, and then it can continue to compiler the code, with an implementation-specific meaning. – Pete Becker Apr 26 '21 at 16:23