Cannot create constexpr std::vector

前端 未结 3 1403
别跟我提以往
别跟我提以往 2021-01-01 10:21

I can create constexpr std::array:

constexpr std::array values {1,2,3,4,5};

It works fine. But I cannot create

3条回答
  •  遥遥无期
    2021-01-01 11:19

    For c++ version at least prior C++2a:

    std::vector uses a dynamic memory allocation. Operator new can't be used in constexpr methods, thus std::vector will never be constexpr, constexpr constructor can't be declared for it. std::array doesn't use dynamic memory allocation, it is allocated in stack. It has no any problem with rules of creation constexpr objects and can be constexpr.

提交回复
热议问题