当前位置: 动力学知识库 > 问答 > 编程问答 >

c++ - Return value of non-member std::empty overload for fixed-size arrays

问题描述:

The [accepted] standards paper N4280 adds several new non-member functions to be folded into C++17.

There are remarks indicating the proper return values for each of the new non-member functions, and in particular, the std::empty overload for fixed-size arrays confuses me. The paper suggests that the return value for that overload will always be false; that all fixed-sized arrays will never be empty.

Why would this be the case? To my knowledge it is possible (though perhaps useless) to have a zero-sized array, as in int x[0];, which I would call empty.

For reference, the particular overload I speak of is:

template <class T, size_t N> constexpr bool empty(const T (&array)[N]) noexcept;

网友答案:

It will always return false simply because arrays can't be declared to contain zero elements. From the C++11 standard (I don't have a more recent standard handy and I don't expect this will have changed anyway):

In a declaration T D where D has the form

    D1 [ constant-expressionopt ] attribute-specifier-seqopt

... If the constant-expression is present, it shall be an integral constant expression and its value shall be greater than zero. - §8.3.4 [dcl.array]

Therefore this overload would always return true in a conforming implementation.

分享给朋友:
您可能感兴趣的文章:
随机阅读: