C++14 中的可变长度数组? [英] Variable Length Arrays in C++14?

查看:96
本文介绍了C++14 中的可变长度数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

n3639 提议采纳s 到 C++14(至少对于第一维.)

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

但是 我最新的能够找到列出n3639为:

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

C++14 的第一张 CD 中的特性,随后被移除到技术规范中

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);
}

这无法在 Visual Studio 2015 和 gcc 中构建(当使用-pedantic"标志时.)

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

在 gcc5.1 下工作正常,但仍然无法在 Visual Studio 2015 下构建.

这只是 gcc 在 C++14 中错误地支持 c99 的可变长度数组,还是以某种方式使它进入 C++14 并且 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?

看起来 gcc 已经取消了对 gcc6.2 的支持:http://coliru.stacked-crooked.com/a/303ae1970fa3f5d2

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

推荐答案

首先,n3639 正在寻找使用运行时绑定 (ARB) 而不是可变长度数组 (VLA) 的数组.ARB 将支持排除的 VLA 子集:

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:

  • 多维数组,其中除顶层之外还有运行时绑定(类似地,array-new 也不支持)
  • 对函数声明符语法的修改
  • sizeof(a) 是一个运行时评估的表达式,返回 a
  • 的大小
  • typedef int a[n]; 评估 n 并通过 typedef
  • 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 月在华盛顿州伊瑟阔举行的标准委员会 一致投票n3820,它的初始版本源自 n3639 和 Dynarrays 的提议.

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.

2014 年 5 月 n4043n4050 试图解决一些半编辑问题"分别位于阵列扩展技术规范的 Dynarray 和 ARB 部分.

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.

但是 标准委员会 2014 年 10 月 24 日的电话会议 引用了关于语言设施、实现可能性和对数组扩展技术规范的渴望的巨大分歧,最终将其描述为处于不确定状态.

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.

标准委员会 2015 年 5 月在 Lenexa 举行的会议, Kansas 继续提供方向性指导,指出当前形式的阵列扩展技术规范不会被接受,并建议:

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:

剥离 TS 的当前内容,等待可行的建议出现[1]

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

最终标准委员会于 2016 年 3 月在佛罗里达州杰克逊维尔在确认一些与阵列相关的提案以图书馆基础技术规范为目标后,决定关闭阵列扩展技术规范.一致投票赞成这样做 8 人强烈赞成,5 人赞成,6 人弃权.

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.

顺便说一下,库基础技术规范中唯一与数组相关的工作是允许通过 make_array.Bjarne Stroustrup,C++ 的创造者,对这个主题进行了雄辩:

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"

遗憾的是,对于 Stroustrup 博士、我们和整个 C++ 社区而言,未来没有计划以简单的 c99 VLA 形式使用 C++ 来复兴 ARB/VLA.

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.

这篇关于C++14 中的可变长度数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆