1

[已接受] 标准文件N4280添加了几个新的非成员函数以折叠到 C++17 中。

有评论指出每个新的非成员函数的正确返回值,特别是std::empty固定大小数组的重载让我感到困惑。该论文建议该重载的返回值将始终为false; 所有固定大小数组永远不会为空。

为什么会这样?据我所知,有可能(尽管可能没用)有一个大小为零的数组,如int x[0];,我称之为空的。

作为参考,我所说的特定过载是:

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

1 回答 1

4

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.

于 2014-12-24T07:14:57.337 回答