为什么可变长度数组不是 C++ 标准的一部分? [英] Why aren't variable-length arrays part of the C++ standard?

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

问题描述

最近几年我很少使用C.当我阅读 this问题 今天我遇到了一些我不熟悉的 C 语法.

I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.

显然在 C99 中,以下语法是有效的:

Apparently in C99 the following syntax is valid:

void foo(int n) {
    int values[n]; //Declare a variable length array
}

这似乎是一个非常有用的功能.是否曾讨论过将其添加到 C++ 标准中,如果有,为什么将其省略?

This seems like a pretty useful feature. Was there ever a discussion about adding it to the C++ standard, and if so, why it was omitted?

一些潜在的原因:

  • 编译器供应商实施的繁琐
  • 与标准的其他部分不兼容
  • 可以使用其他 C++ 构造来模拟功能

C++ 标准规定数组大小必须是常量表达式 (8.3.4.1).

The C++ standard states that array size must be a constant expression (8.3.4.1).

是的,我当然意识到在玩具示例中可以使用 std::vector;values(m);,但这会从堆而不是堆栈中分配内存.如果我想要一个多维数组,例如:

Yes, of course I realize that in the toy example one could use std::vector<int> values(m);, but this allocates memory from the heap and not the stack. And if I want a multidimensional array like:

void foo(int x, int y, int z) {
    int values[x][y][z]; // Declare a variable length array
}

vector 版本变得非常笨拙:

void foo(int x, int y, int z) {
    vector< vector< vector<int> > > values( /* Really painful expression here. */);
}

切片、行和列也可能分布在整个内存中.

The slices, rows and columns will also potentially be spread all over memory.

查看 comp.std.c++ 上的讨论,很明显这个问题很有争议,争论双方都有一些非常重量级的名字.std::vector 总是更好的解决方案当然不是很明显.

Looking at the discussion at comp.std.c++ it's clear that this question is pretty controversial with some very heavyweight names on both sides of the argument. It's certainly not obvious that a std::vector is always a better solution.

推荐答案

最近在 usenet 上有一个关于这个的讨论:为什么 C++0x 中没有 VLA.

There recently was a discussion about this kicked off in usenet: Why no VLAs in C++0x.

我同意那些似乎同意必须在堆栈上创建一个潜在的大型数组的人的看法,该数组通常只有很少的可用空间,这是不好的.参数是,如果您事先知道大小,则可以使用静态数组.而且如果事先不知道大小,就会写出不安全的代码.

I agree with those people that seem to agree that having to create a potential large array on the stack, which usually has only little space available, isn't good. The argument is, if you know the size beforehand, you can use a static array. And if you don't know the size beforehand, you will write unsafe code.

C99 VLA 可以提供一个小的好处,即能够创建小数组而不浪费空间或为未使用的元素调用构造函数,但它们会给类型系统带来相当大的变化(您需要能够根据运行时指定类型值 - 除了 new 运算符类型说明符之外,当前 C++ 中尚不存在此值,但它们被特殊对待,因此运行时性不会超出 new 的范围 运算符).

C99 VLAs could provide a small benefit of being able to create small arrays without wasting space or calling constructors for unused elements, but they will introduce rather large changes to the type system (you need to be able to specify types depending on runtime values - this does not yet exist in current C++, except for new operator type-specifiers, but they are treated specially, so that the runtime-ness doesn't escape the scope of the new operator).

您可以使用 std::vector,但它并不完全相同,因为它使用动态内存,并且使其使用自己的堆栈分配器并不容易(对齐是也有问题).它也没有解决同样的问题,因为向量是一个可调整大小的容器,而 VLA 是固定大小的.C++ 动态数组 提案旨在引入基于库的解决方案,作为基于语言的 VLA 的替代方案.但是,据我所知,它不会成为 C++0x 的一部分.

You can use std::vector, but it is not quite the same, as it uses dynamic memory, and making it use one's own stack-allocator isn't exactly easy (alignment is an issue, too). It also doesn't solve the same problem, because a vector is a resizable container, whereas VLAs are fixed-size. The C++ Dynamic Array proposal is intended to introduce a library based solution, as alternative to a language based VLA. However, it's not going to be part of C++0x, as far as I know.

这篇关于为什么可变长度数组不是 C++ 标准的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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