I'm new to the use of the noexcept
specifier and I do not understand why std::array::front
and std::array::back
are not declared noexcept
(whereas std::array::begin
and std::array::end
are).
What is the reason of that?
I'm new to the use of the noexcept
specifier and I do not understand why std::array::front
and std::array::back
are not declared noexcept
(whereas std::array::begin
and std::array::end
are).
What is the reason of that?
From 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.
So since we can have a 0 sized array front()
and back()
could cause an exception
To quote 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.