简体   繁体   English

为什么在C ++ 14中运行时大小的数组和std :: dynarray?

[英]Why both runtime-sized arrays and std::dynarray in C++14?

Draft C++14 includes both runtime-sized arrays and the std::dynarray container. Draft C ++ 14包括运行时大小的数组和std::dynarray容器。 From what I can tell, the only real difference between the two is that std::dynarray has an STL interface (eg, begin , end , size , etc.), while runtime-sized arrays do not. 据我所知,两者之间唯一真正的区别是std::dynarray有一个STL接口(例如, beginendsize等),而运行时大小的数组则没有。 So why does C++14 need them both? 那么为什么C ++ 14需要它们呢?

I understand that runtime-sized arrays are part of the core language, while std::dynarray is part of the standard library, but the proposal for std::dynarray makes clear that the authors expect compilers, in many cases, to offer special support for std::dynarray so that it can be as efficient as possible, ie, as efficient as a runtime-sized array. 我知道运行时大小的数组是核心语言的一部分,而std::dynarray是标准库的一部分,但std::dynarray的提议清楚地表明作者希望编译器在许多情况下提供特殊支持对于std::dynarray ,它可以尽可能高效,即与运行时大小的数组一样高效。 As such, the language/library distinction seems somewhat artificial. 因此,语言/库的区别似乎有点人为。

So, again, why does C++14 need both runtime-sized arrays and std::dynarray ? 那么,为什么C ++ 14需要运行时大小的数组和std::dynarray呢? And given that std::dynarray has a richer (STLified) interface, why not just drop runtime-sized arrays, assuming that std::dynarray can be implemented with equal runtime efficiency? 并且鉴于std::dynarray具有更丰富的(STLified)接口,为什么不只是删除运行时大小的数组,假设std::dynarray可以以相同的运行时效率实现?

Clarification 澄清

When I talk about "runtime-sized arrays," I'm referring to a new C++14 core language feature that's described in N3639 , not to traditional C arrays or VLAs or anything in C++11. 当我谈到“运行时大小的数组”时,我指的是N3639中描述的新的C ++ 14核心语言特性,而不是传统的C数组或VLA或C ++ 11中的任何内容。

N3639 proposes to add local runtime-sized arrays with automatic storage duration to C++. N3639建议将具有自动存储持续时间的本地运行时大小的数组添加到C ++。

N2648 says that in keeping with C++ practice, std::dynarray s are usable with more than just automatic variables. N2648表示,为了与C ++实践保持一致, std::dynarray可用于不仅仅是自动变量。 But to take advantage of the efficiency stack allocation, we wish to make dynarray optimizable when used as an automatic variable. 但是为了利用效率堆栈分配,我们希望在用作自动变量时使dynarray优化。

In short, C11 style runtime-sized arrays are restricted to being stored on the stack. 简而言之,C11样式的运行时大小的数组仅限于存储在堆栈中。 dynarray is not, but can be optimized when stored on the stack to be as efficient as C11 style runtime-sized arrays (or so is the goal). dynarray不是,但可以在存储在堆栈上时进行优化,以便与C11样式的运行时大小的数组一样高效(目标是左右)。

C11 style runtime-sized arrays can be a useful syntax still, and the cost to increase intercompilability with C isn't high: the mechanism would have to be implemented for efficient automatic dynarray anyhow. C11样式的运行时大小的数组仍然是一个有用的语法,并且增加与C的可dynarray的成本并不高:无论如何必须实现高效自动dynarray的机制。 In addition, C11 style runtime-sized arrays are first class citizens, and exist regardless of use of std libraries by the programmer. 此外,C11样式的运行时大小的数组是一等公民,并且无论程序员使用std库如何都存在。

There are important differences between actual C11 runtime-sized arrays and C++1y C11-style runtime-sized arrays, not the least of which is the runtime sizeof that actual C11 runtime-sized arrays support. 有实际运行时C11大小的数组和C ++ 1Y C11式运行时尺寸的阵列,而不是其中的是运行时间之间的重要区别sizeof ,实际的运行时间C11尺寸阵列的支持。 But basic use of it may be compatible. 但它的基本用途可能是兼容的。

Note that in the end, neither where added in C++14. 请注意,最后,在C ++ 14中都没有添加。

As you said yourself std::dynarray will provide STL-style interface, which makes it more idiomatic to use. 正如你所说, std::dynarray将提供STL风格的界面,这使得它更加惯用。 Still, C++ needs dynamic arrays created with new[] to: 但是,C ++需要使用new[]创建的动态数组:

  1. at least implement std::dynarray (so you can't have dynarray without new[]) 至少实现std::dynarray (所以没有new []就不能有dynarray)
  2. retain compatibility with previous versions 保持与先前版本的兼容性

You can not just say that all code, which uses new[] is now wrong. 你不能只说所有使用new []的代码现在都是错误的。

In general, the difference between C++14 std::dynarray and C++ new[] array is almost the same as difference between C++11 std::array and C-style arrays. 通常,C ++ 14 std::dynarray和C ++ new[]数组之间的差异几乎与C ++ 11 std::array和C风格数组之间的差异相同。

UPD: Now I see you are now asking about feature similar to C11 (VLA's). UPD:现在我看到你现在正在询问类似于C11(VLA)的功能。 Actually there is nothing to do with it - VLA's are very limited and you can use only an argument of the function as your array size. 实际上它没有任何关系--VLA是非常有限的,你只能使用函数的参数作为你的数组大小。 Also, memory is allocated on stack, but for std::dynarray memory is allocated in the heap. 此外,内存在堆栈上分配,但对于std::dynarray内存在堆中分配。 Basically, this feature just extends C-style arrays a little bit more and makes C++ a bit more compatible with modern C standard. 基本上,这个功能只是扩展了C风格的数组,使C ++与现代C标准更加兼容。

I think you answered the question yourself, std::dynarray has the stl interface. 我想你自己回答了这个问题, std::dynarray有stl接口。 A goal of c++11 and I'm assuming c++14 is to make c++ more user friendly, less error prone and easier for beginners. c ++ 11的一个目标,我假设c ++ 14是为了让c ++更加用户友好,更不容易出错,对初学者来说更容易。 With c style arrays you may run into pointer arithmetic problems but dynarray avoids the problems if used as intended 对于c样式数组,您可能遇到指针算术问题,但如果按预期使用, dynarray可以避免出现问题
EDIT: so it looks like one difference is that runtime-sized arrays must be allocated on the stack, increasing the likelyhood of a stack overflow. 编辑:所以看起来一个区别是必须在堆栈上分配运行时大小的数组,增加堆栈溢出的可能性。 dynarray is allocated on the heap though it is possible to allocate on the stack (if the implementation did so) 虽然可以在堆栈上分配(如果实现这样做),则在堆上分配dynarray

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM