Why do we need runtime size arrays and std :: dynarray in C ++ 14?

The C ++ 14 project includes both runtime size arrays and the std::dynarray . 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 size arrays don't work . So why does C ++ 14 need both of them?

I understand that runtime size arrays are part of the main language, and std::dynarray is part of the standard library, but the suggestion for std::dynarray clear that authors expect compilers to offer special support for std::dynarray in many cases, so that it can be as efficient as possible, that is, as efficient as the size of the runtime array. So the language / library distinction seems somewhat artificial.

So again, why does C ++ 14 need both runtime size arrays and std::dynarray ? And given that std::dynarray has a richer (STLified) interface, why not just drop runtime arrays, assuming std::dynarray can be implemented with equal execution efficiency?

Explanation

When I talk about "runtime size arrays", I mean the new C ++ 14 language feature described in N3639 , not traditional C-arrays or VLA or anything in C ++ 11.

+44
c ++ c ++ 14 dynamic-arrays
Jun 27 '13 at 21:54
source share
3 answers

N3639 suggests adding local runtime arrays with automatic storage duration up to C ++.

N2648 says that according to C ++ practice, std::dynarray can be used not only with automatic variables. But to take advantage of the distribution of the efficiency stack, we want to optimize dynarray when used as an automatic variable.

In short, C11 runtime arrays are limited to being stored on the stack. dynarray not, but can be optimized when stored on the stack as efficiently as in arrays of runtime size (e.g. C11).

Arrays with C11 runtime sizes can be useful syntax, and the cost of increasing C compatibility is small: a mechanism must be implemented for efficient automatic dynarray . In addition, C11 runtime arrays are first class citizens and exist regardless of whether the programmer uses the std library.

There are significant differences between real C11 runtime size arrays and C ++ 1y C11 runtime size arrays, not the least of which is sizeof runtime, which supports actual support for C11 runtime arrays. But its main use can be compatible.

Note that in the end is not what is added in C ++ 14.

+30
Jun 27 '13 at 22:12
source share

As you said, std::dynarray will provide an STL-style interface, which makes it more idiomatic for use. However, C ++ needs dynamic arrays created with new[] to:

  • at least implement std::dynarray (so you cannot have dynarray without new [])
  • keep compatibility with previous versions

You cannot just say that all the code that uses the new [] is now incorrect.

In general, the difference between C ++ 14 std::dynarray and C ++ new[] arrays is almost the same as the difference between C ++ 11 std::array and C-style arrays.

UPD: Now I see that you are now asking about a function similar to C11 (VLA). This actually has nothing to do - VLAs are very limited, and you can only use function arguments as the size of your array. In addition, memory is allocated on the stack, but for std::dynarray memory is allocated on the heap. Basically, this function only slightly expands C-style arrays and makes C ++ more compatible with the modern C standard.

+2
Jun 27 '13 at 10:01
source share

I think you yourself answered the question, std::dynarray has a stl interface. The goal of C ++ 11, and I assume that C ++ 14 is to make C ++ more user friendly, less error prone and easier for beginners. With arrays of type c, you might run into pointer arithmetic problems, but dynarray avoids the problems if they are used as intended EDITs: it seems the one difference is that runtime size arrays should be allocated on the stack, increasing the likelihood. dynarray is allocated on the heap, although it can be allocated on the stack (if the implementation did this)

+2
Jun 27 '13 at 10:01
source share



All Articles