C++ Logo

std-proposals

Advanced search

Re: [std-proposals] vector::push_back must not invalidate past-the-end iterator

From: Nikl Kelbon <kelbonage_at_[hidden]>
Date: Mon, 8 Dec 2025 23:42:12 +0500
About this:

> There is no other container where getting an end iterator and
inserting an element into the end of the container would *ever* leave
that end iterator to be pointing to something valid. Not `list`, not
`deque`

https://godbolt.org/z/ndePjE9c6

It works for deque. Its possible

#include <deque>
#include <iostream>

int main() {
  std::deque<int> v;
  v.push_back(0);
  for (int i = 0; i < 999; ++i) {
    auto b = std::prev(v.end());
    auto e = v.end();
    v.push_back(i);
    ++b;
    if (b == e)
       std::cout << "yes" << std::endl;
    else
       std::cout << "no" << std::endl;
  }
}

Received on 2025-12-08 18:42:25