9

I'm just jumping into C++, coming from C

In C (89/90), a const is not actually a constant (as opposed to a #define'd, enum, or literal), but rather read-only once set. I.e, I can:

const int x = rand();

and that's fine - the point being x isn't known until runtime. Hence, I can't

int arr[x]; // error - x is not a compile-time constant

Then, one of the C standards (99?) went ahead and allowed for variable-length arrays. Although I normally code against the ANSI standard in C, this has actually had an impact now that I am trying to pickup C++11.

As far as I know, C++ does not allow for variable-length arrays. However, many compilers allow it as an extension (GCC ?). The problem is, now that I am trying to learn C++11, I can't tell if what I'm coding is valid C++, or C++ extended with C99-compatibility. Ex:

std::default_random_engine e{};
std::uniform_int_distribution<int> d{};
const int x{d(e)};
int arr[x]; // compiles

I can't tell if this is valid C++ or not. Clearly, the value of x is not known until runtime. I think I may not understand the difference between C and C++ const?

zac
  • 847
  • 6
  • 17
  • Not valid. Use flags `-std=...` where `...` is a standard, e.g. `c++11` or `c++98`. – juanchopanza Sep 19 '14 at 17:24
  • 2
    You may also use `-pedantic` – Jarod42 Sep 19 '14 at 17:27
  • @juanchopanza I actually did compile using `-std=c++11` and clang produced no warnings. Using the `-pedantic` flag as Shafik suggested made the warnings pop up! – zac Sep 19 '14 at 17:28
  • Not valid in C++11, but C++14 allows stack arrays with runtime sizes – Johannes Schaub - litb Sep 19 '14 at 17:52
  • 1
    @JohannesSchaub-litb, The Array Extensions TS didn't make it into C++14. – chris Sep 19 '14 at 17:55
  • @chris someone is looking out for us – zac Sep 19 '14 at 17:57
  • As was mentioned, you **can* make variable-sized array, using `int* arr = new int[x];`, and `delete[] arr;` when `arr` is no longer needed, or, better, using [`std::vector`](http://www.cplusplus.com/reference/vector/vector/) – GingerPlusPlus Sep 19 '14 at 18:16
  • This is somewhat related question: [Does “int size = 10;” yield a constant expression?](http://stackoverflow.com/q/21273829/1708801) which may be interesting since my answer to that question goes into details about what is considered an integral constant expression. Which is what an array bound requires. – Shafik Yaghmour Sep 19 '14 at 18:31
  • @chris do you mean that the "array extensions TS" covers the runtime sizes array? I am looking into 8.3.4 of a 2013 draft (n3690) and it talks about "array of runtime bound of T". Isn't that the thing I was talking about? – Johannes Schaub - litb Sep 19 '14 at 18:46
  • @JohannesSchaub-litb, As I recall, they were in, but got pulled out. I don't see them in N3797 at all. There's a [Rapperswil trip report](http://botondballo.wordpress.com/2014/07/17/trip-report-c-standards-committee-meeting-in-rapperswil-june-2014/) that has more information on its status. – chris Sep 19 '14 at 18:51

1 Answers1

11

You are correct VLAs are a C99 feature(optional in C11) and the C++ standard does not include this feature although both gcc and clang allow them in C++ as an extension. We can see they are not allowed by going to the draft C++11 standard section 8.3.4 Arrays which says:

D1 [ constant-expressionopt] attribute-specifier-seqopt
     ^^^^^^^^^^^^^^^^^^^^^^

For both gcc and clang using the -pedantic flag will warn when you are using an extension. If you are targeting C++11 then you should also specify that using -std=c++11. You can use -pedantic-errors to turn the warning into errors. If you compile your code using -pedantic you should see the the following warning:

warning: ISO C++ forbids variable length array 'arr' [-Wvla]
int arr[x]; // compiles
         ^

gcc documents their support for various standards, defaults and flags to enforce standard on their Language Standards Supported by GCC page and it says:

to obtain all the diagnostics required by the standard, you should also specify -pedantic (or -pedantic-errors if you want them to be errors rather than warnings).

In general clang supports what gcc does but you can find more details on their Language Compatibility page.

Note as mentioned by GingerPlusPlus std:vector is considered the alternative for VLA in C++.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740