std::dynarray vs std::vector

后端 未结 2 970
鱼传尺愫
鱼传尺愫 2020-12-02 16:27

C++14 presents std::dynarray:

std::dynarray is a sequence container that encapsulates arrays with a size that is fixed at construction and does not

2条回答
  •  悲哀的现实
    2020-12-02 17:11

    So what are the benefits and the usage of std::dynarray, when we can use std::vector which is more dynamic (Re-sizable)?

    dynarray is smaller and simpler than vector, because it doesn't need to manage separate size and capacity values, and it doesn't need to store an allocator.

    However the main performance benefit is intended to come from the fact that implementations are encouraged to allocate dynarray on the stack when possible, avoiding any heap allocation. e.g.

    std::dynarray d(5);   // can use stack memory for elements
    auto p = new std::dynarray(6);  // must use heap memory for elements
    

    This optimisation requires cooperation from the compiler, it can't be implemented as a pure library type, and the necessary compiler magic has not been implemented and noone is sure how easy it is to do. Because of the lack of implementation experience, at the C++ committee meeting in Chicago last week it was decided to pull std::dynarray from C++14 and to issue a separate array extensions TS (technical specification) document defining std::experimental::dynarray and arrays of runtime bound (ARBs, similar to C99 VLAs.) This means std::dynarray will almost certainly not be in C++14.

提交回复
热议问题