Using std::extent on std::array

后端 未结 4 1706
名媛妹妹
名媛妹妹 2021-01-13 08:21

I have a templatized function and I want to static_assert that it\'s type has a size of three. This code illustrates what I\'m trying to do, but doesn\'t work:<

4条回答
  •  鱼传尺愫
    2021-01-13 08:56

    I suspect you're having this error because at compile time (which is when *static_assert* is evaluated) the dimensions of cArray and stdArray are unknown, and std::extent returns zero:

    http://en.cppreference.com/w/cpp/types/extent

    In fact, the error disappears if you change the condition to equal to zero:

    static_assert( std::extent< T >::value == 0, "param is not zero" );
    

    Nevertheless, as it has already been pointed out in other posts, std::extent won't work for std::array, only for built in arrays such as cArray

    AFTER OP's EDIT: Now that you have modified foo to accept a reference, you'll have that std::extent reports 3 for cArray and 0 (zero) for stdArray. Hence, you'll carry on having the exception at compile time, because the dimension of stdArray is not 3.

提交回复
热议问题