Search code examples
c++stlerase

vec.erase(vec.end()); Legal?


Can sequential containers erase end?

In the standard it says:

a.erase(q) 
Requires: For vector and deque, T shall be
MoveAssignable.
Effects: Erases the element pointed to by q

It is not clear if a.erase(a.end()) is a no-op or UB for sequential containers. Ideas?


Solution

  • §23.2.3 [sequence.reqmts]/p3 (emphasis mine):

    In Tables 100 and 101, X denotes a sequence container class, a denotes a value of X containing elements of type T, [...], q denotes a valid dereferenceable const iterator to a.

    Table 100 is the sequence container requirements table containing a.erase(q).

    In other words, a.erase(a.end()) is UB.