Variable Length Arrays in C++14?

时光总嘲笑我的痴心妄想 提交于 2019-12-18 04:54:22

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/40633344/variable-length-arrays-in-c14

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!