我不熟悉 noexcept
说明符的使用,我不明白为什么 std::array::front
和 std::array: :back
未声明 noexcept
(而 std::array::begin
和 std::array::end
是) .
这是什么原因?
最佳答案
来自 cppreference
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在 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.
关于c++ - 为什么 std::array::front 和 std::array::back 不是 noexcept?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32635659/