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

前端 未结 3 482
北荒
北荒 2020-12-16 09:17

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 st

3条回答
  •  星月不相逢
    2020-12-16 09:36

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

    1. at least implement std::dynarray (so you can't have dynarray without new[])
    2. retain compatibility with previous versions

    You can not just say that all code, which uses new[] is now wrong.

    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.

    UPD: Now I see you are now asking about feature similar to C11 (VLA's). 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. Also, memory is allocated on stack, but for std::dynarray memory is allocated in the heap. Basically, this feature just extends C-style arrays a little bit more and makes C++ a bit more compatible with modern C standard.

提交回复
热议问题