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

前端 未结 4 959
礼貌的吻别
礼貌的吻别 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:21

    You can use the same template-inference method as has always been used for C++98 array bound detection.

    template
    constant_integer array_size( const std::array& );
    
    • Demo: http://ideone.com/R4k1vG

    Make a nice macro wrapper and enjoy!

    Many variations are also possible, such as:

    • http://ideone.com/Hn46ei

提交回复
热议问题