Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Draft C++14 includes both runtime-sized arrays and the std::dynarray container. From what I can tell, the only real difference between the two is that std::dynarray has an STL interface (e.g., begin, end, size, etc.), while runtime-sized arrays do not. So why does C++14 need them both?

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, i.e., as efficient as a runtime-sized array. As such, the language/library distinction seems somewhat artificial.

So, again, why does C++14 need both runtime-sized arrays and 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?

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.

like image 679
KnowItAllWannabe Avatar asked Jun 27 '13 21:06

KnowItAllWannabe


2 Answers

N3639 proposes to add local runtime-sized arrays with automatic storage duration to C++.

N2648 says that in keeping with C++ practice, std::dynarrays are usable with more than just automatic variables. But to take advantage of the efficiency stack allocation, we wish to make dynarray optimizable when used as an automatic variable.

In short, C11 style runtime-sized arrays are restricted to being stored on the stack. 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).

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. In addition, C11 style runtime-sized arrays are first class citizens, and exist regardless of use of std libraries by the programmer.

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. But basic use of it may be compatible.

Note that in the end, neither where added in C++14.

like image 69
Yakk - Adam Nevraumont Avatar answered Sep 20 '22 02:09

Yakk - Adam Nevraumont


I think you answered the question yourself, std::dynarray has the stl interface. 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. With c style arrays you may run into pointer arithmetic problems but dynarray avoids the problems if used as intended
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)

like image 28
aaronman Avatar answered Sep 22 '22 02:09

aaronman