Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable Length Arrays in C++14?

n3639 proposed the adoption of c99's variable-length-arrays into C++14 (at least for the first dimension.)

But the latest I've been able to find lists n3639 as:

Features in the first CD of C++14, subsequently removed to a Technical Specification

Did this ever make it into a Technical Specification, or was it lost in the hand off?

The reason for my question is, I've noticed this code:

void f(size_t n) {
    int a[n];
    for (size_t i = 0; i < n; ++i)
        a[i] = 2 * i;
    sort(a, a + n);
}

This fails to build in Visual Studio 2015 and in gcc (when the "-pedantic" flag is used.)

Works fine under gcc5.1, but still fails to build under Visual Studio 2015.

Is this just gcc incorrectly supporting c99's Variable Length Arrays in C++14 or did this somehow make it into C++14 and Visual Studio 2015 failed to pick it up?

EDIT: It looks like gcc has removed support in gcc6.2: http://coliru.stacked-crooked.com/a/303ae1970fa3f5d2

like image 971
Jonathan Mee Avatar asked Nov 16 '16 13:11

Jonathan Mee


People also ask

How do I make a variable size array in C++?

If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself: int n = 10; double* a = new double[n]; // Don't forget to delete [] a; when you're done!

Does C++ allow variable length arrays?

But C++ standard (till C++11) doesn't support variable sized arrays. The C++11 standard mentions array size as a constant-expression.

How do variable length arrays work in C?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).

How do you find the length of an array in C?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);


1 Answers

First of all, n3639 was looking to put in place Arrays with Runtime Bound (ARB) not Variable Length Arrays (VLA). ARBs would support a subset of VLAs which excluded:

  • multidimensional arrays, where other than the top level has a runtime bound (in analogy, array-new doesn't support that either)
  • modifications to the function declarator syntax
  • sizeof(a) being a runtime-evaluated expression returning the size of a
  • typedef int a[n]; evaluating n and passing that through the typedef

In February of 2014 in Issaquah, Washington, at the standard committee unanimously voted to form the Array Extensions Technical Specification from n3820, it's initial revision originated from n3639 and the proposal of Dynarrays.

In May 2014 n4043 and n4050 attempted to address some "semi-editorial issues" in the Dynarray and ARB sections of the Array Extension Technical Specification, respectively.

But the standard committee's October 24 2014 teleconference cited huge disagreement on the language facilities, implementation possibilities, and desire for Array Extensions Technical Specification, ultimately describing it as in a state of limbo.

The standard committee's May 2015 meeting in Lenexa, Kansas went on to give the directional guidance that the Array Extensions Technical Specification would not be accepted in it's current form, and recommended:

Stripping the TS of its current contents, and waiting for a workable proposal to come along[1]

Ultimately the standard committee's March 2016 meeting in Jacksonville, Florida moved to close the Array Extensions Technical Specification at the confirmation that some array-related proposals are targeting the Library Fundamentals Technical Specification instead. There was a unanimous vote to do so with 8 strongly in favor, 5 in favor, and 6 abstaining.

Incidentally the only array related work going into the Library Fundamentals Technical Specification is the allowance of run-time creation of an array via make_array. Bjarne Stroustrup, the creator of C++, waxed eloquent on the topic:

We need arrays with run-time-specified bounds and safer access to such storage “yesterday”

Sadly, for Dr. Stroustrup, us, and the C++ community as a whole, there are no future plans to resurrect ARBs/VLAs with C++ in the simple c99 VLA form.

like image 171
Jonathan Mee Avatar answered Sep 21 '22 13:09

Jonathan Mee