Why are std::array::front and std::array::back not noexcept? Why are std::array::front and std::array::back not noexcept? arrays arrays

Why are std::array::front and std::array::back not noexcept?


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 calling front() or back() 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 not noexcept, even when they are not specified to throw.