C++14 中的可变长度数组?
Variable Length Arrays in C++14?
n3639 proposed the adoption of c99's variable-length-arrays 进入 C++14(至少对于第一个维度。)
但是 latest I've been able to find 将 n3639 列为:
Features in the first CD of C++14, subsequently removed to a Technical Specification
这有没有成为技术规范,还是在交付过程中丢失了?
我提出问题的原因是,我注意到这段代码:
void f(size_t n) {
int a[n];
for (size_t i = 0; i < n; ++i)
a[i] = 2 * i;
sort(a, a + n);
}
这无法在 Visual Studio 2015 和 gcc 中构建(当使用“-pedantic”标志时。)
Works fine under gcc5.1, but still fails to build 下 Visual Studio 2015.
这只是 gcc 在 C++14 中错误地支持了 c99 的可变长度数组,还是它以某种方式进入了 C++14 而 Visual Studio 2015 未能将其提取出来?
编辑: 看起来 gcc 已经删除了 gcc6.2 中的支持:http://coliru.stacked-crooked.com/a/303ae1970fa3f5d2
首先,n3639 希望放置运行时绑定 (ARB) 而非可变长度数组 (VLA) 的数组。 ARB 将支持 VLA 的子集,其中不包括:
- 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
2014 年 2 月在华盛顿州伊瑟阔,标准委员会 unanimously voted to form the Array Extensions Technical Specification from n3820, it's initial revision originated from n3639 and the proposal of Dynarrays。
2014 年 5 月,n4043 and n4050 尝试分别解决数组扩展技术规范的 Dynarray 和 ARB 部分中的某些 "semi-editorial issues"。
但是 the standard committee's October 24 2014 teleconference 在语言设施、实现可能性以及对数组扩展技术规范的需求方面存在巨大分歧,最终将其描述为处于不确定状态。
The standard committee's May 2015 meeting in Lenexa, Kansas 继续给出阵列扩展技术规范不会以其当前形式被接受的方向性指导,并建议:
Stripping the TS of its current contents, and waiting for a workable proposal to come along[1]
最终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 so8票强烈赞成,5票赞成,6票弃权。
顺便说一下,图书馆基础技术规范中唯一与数组相关的工作是允许通过 make_array
. Bjarne Stroustrup, the creator of C++, waxed eloquent on the topic:
运行 时间创建 array
We need arrays with run-time-specified bounds and safer access to such storage “yesterday”
可悲的是,对于 Stroustrup 博士、我们和整个 C++ 社区来说,未来没有计划以简单的 c99 VLA 形式使用 C++ 复活 ARBs/VLAs。
n3639 proposed the adoption of c99's variable-length-arrays 进入 C++14(至少对于第一个维度。)
但是 latest I've been able to find 将 n3639 列为:
Features in the first CD of C++14, subsequently removed to a Technical Specification
这有没有成为技术规范,还是在交付过程中丢失了?
我提出问题的原因是,我注意到这段代码:
void f(size_t n) {
int a[n];
for (size_t i = 0; i < n; ++i)
a[i] = 2 * i;
sort(a, a + n);
}
这无法在 Visual Studio 2015 和 gcc 中构建(当使用“-pedantic”标志时。)
Works fine under gcc5.1, but still fails to build 下 Visual Studio 2015.
这只是 gcc 在 C++14 中错误地支持了 c99 的可变长度数组,还是它以某种方式进入了 C++14 而 Visual Studio 2015 未能将其提取出来?
编辑: 看起来 gcc 已经删除了 gcc6.2 中的支持:http://coliru.stacked-crooked.com/a/303ae1970fa3f5d2
首先,n3639 希望放置运行时绑定 (ARB) 而非可变长度数组 (VLA) 的数组。 ARB 将支持 VLA 的子集,其中不包括:
- 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 ofa
typedef int a[n];
evaluatingn
and passing that through thetypedef
2014 年 2 月在华盛顿州伊瑟阔,标准委员会 unanimously voted to form the Array Extensions Technical Specification from n3820, it's initial revision originated from n3639 and the proposal of Dynarrays。
2014 年 5 月,n4043 and n4050 尝试分别解决数组扩展技术规范的 Dynarray 和 ARB 部分中的某些 "semi-editorial issues"。
但是 the standard committee's October 24 2014 teleconference 在语言设施、实现可能性以及对数组扩展技术规范的需求方面存在巨大分歧,最终将其描述为处于不确定状态。
The standard committee's May 2015 meeting in Lenexa, Kansas 继续给出阵列扩展技术规范不会以其当前形式被接受的方向性指导,并建议:
Stripping the TS of its current contents, and waiting for a workable proposal to come along[1]
最终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 so8票强烈赞成,5票赞成,6票弃权。
顺便说一下,图书馆基础技术规范中唯一与数组相关的工作是允许通过 make_array
. Bjarne Stroustrup, the creator of C++, waxed eloquent on the topic:
array
We need arrays with run-time-specified bounds and safer access to such storage “yesterday”
可悲的是,对于 Stroustrup 博士、我们和整个 C++ 社区来说,未来没有计划以简单的 c99 VLA 形式使用 C++ 复活 ARBs/VLAs。