Getting the number of elements in std::array at compile time

前端 未结 4 964
礼貌的吻别
礼貌的吻别 2021-02-06 23:45

Is the following a valid C++ code, and why not?

std::array a1;
std::array a2;

It doesn\'t compile

4条回答
  •  悲哀的现实
    2021-02-07 00:19

    std::array::size is actually required to be constexpr per § 23.3.2.1 of the C++11 standard:

    23.3.2.4 array::size [array.size]  
    template  constexpr size_type array::size() noexcept;  
    Returns: N
    

    I'm guessing this just slipped past whoever implemented it in GCC.


    After testing, this works:

    std::array a1;
    std::array a2;
    

    This may actually have something to do with std::string not being a valid constexpr type to make compile-time instances of, whereas int is.

提交回复
热议问题