为什么 std::array::front 和 std::array::back 不是 noexcept?
Why are std::array::front and std::array::back not noexcept?
我是 noexcept
说明符的新手,我不明白为什么 std::array::front
和 std::array::back
没有声明 noexcept
(而 std::array::begin
和 std::array::end
是)。
这是什么原因?
There is a special case for a zero-length array (N == 0)
. In that case, array.begin() == array.end()
, which is some unique value. The effect of calling front()
or back()
on a zero-sized array is undefined.
所以因为我们可以有一个 0 大小的数组 front()
并且 back()
可能会导致异常
引用Sebastian Redl on why the standard doesn't mandate operator[]
, front
and back
be marked noexcept
The standard's policy on noexcept
is to only mark functions that cannot or must not fail, but not those that simply are specified not to throw exceptions. In other words, all functions that have a limited domain (pass the wrong arguments and you get undefined behavior) are not noexcept
, even when they are not specified to throw.
我是 noexcept
说明符的新手,我不明白为什么 std::array::front
和 std::array::back
没有声明 noexcept
(而 std::array::begin
和 std::array::end
是)。
这是什么原因?
There is a special case for a zero-length array
(N == 0)
. In that case,array.begin() == array.end()
, which is some unique value. The effect of callingfront()
orback()
on a zero-sized array is undefined.
所以因为我们可以有一个 0 大小的数组 front()
并且 back()
可能会导致异常
引用Sebastian Redl on why the standard doesn't mandate operator[]
, front
and back
be marked noexcept
The standard's policy on
noexcept
is to only mark functions that cannot or must not fail, but not those that simply are specified not to throw exceptions. In other words, all functions that have a limited domain (pass the wrong arguments and you get undefined behavior) are notnoexcept
, even when they are not specified to throw.