Search code examples
c++language-lawyerstdvector

Is begin() == end() for any empty() vector?


I have long assumed that for any empty std::vector V, V.begin() == V.end(). Yet I see nothing in the C++ specification that states this to always be true. Is it necessarily true or does it just happen to be true on most implementations?


Solution

  • Yes, that's what the standard requires it to be for empty() for any container.

    § 23.2.1 Table 96 of the C++11 standard says:

    Expression Return Type Operational Semantics
    a.empty() Convertible to bool a.begin() == a.end()