Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 659
Vincent Avatar asked Sep 17 '15 16:09

Vincent


People also ask

Is std :: array movable?

std::array is movable only if its contained objects are movable. std::array is quite different from the other containers because the container object contains the storage, not just pointers into the heap. Moving a std::vector only copies some pointers, and the contained objects are none the wiser.

What is the point of std :: array?

std::array provides many benefits over built-in arrays, such as preventing automatic decay into a pointer, maintaining the array size, providing bounds checking, and allowing the use of C++ container operations.

Is std :: array dynamic?

std::array<std::vector<int>,3> is the type you want. std::vector is a dynamicly sized array. this creates a 3 "major" element array of 22 "minor" size.

Is std :: array fixed size?

std::array is a container that encapsulates fixed size arrays. This container is an aggregate type with the same semantics as a struct holding a C-style array T[N] as its only non-static data member. Unlike a C-style array, it doesn't decay to T* automatically.


1 Answers

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.

like image 123
NathanOliver Avatar answered Oct 20 '22 00:10

NathanOliver