[RFC] libstdc++: Leave safe iterators in valid state post-move [PR127540]

Jonathan Wakely jwakely@redhat.com
Fri Oct 16 14:56:53 GMT 2026


On Fri, 16 Oct 2026 at 15:37, Nathan Myers <ncm@cantrip.org> wrote:
>
> On 10/16/26 6:16 AM, Jonathan Wakely wrote:
> > Currently our safe iterators for debug mode fail to meet the requirement
> > that they have a "valid but unspecified" state after being moved from.
> > They have a singular and non-copyable state, which means they cannot be
> > copied or passed around by value. This is non-conforming.
> >
> > Define the moved-from state for our safe iterators to be the same as
> > their value-initialized state. This means they are copyable after being
> > moved from.
> >
> > The downside of this change is that we no longer diagnose equality
> > comparison with a moved-from iterator, which is undefined behaviour.
> > Because value-initialized iterators can be compared, our moved-from
> > iterators are now also comparable. This seems like an acceptable
> > compromise in order to support copying them (which needs to work).
>
> Can we diagnose comparison of a default-initialized iterator to
> anything but another one? It is very likely to be a coding error.

We already do that.

#define _GLIBCXX_DEBUG 1
#include <vector>

int main()
{
  std::vector<int> v{1,2,3};
  auto i = v.begin();
  std::vector<int>::iterator j;
  i == j;
}

/usr/include/c++/15/debug/safe_iterator.h:549:
In function:
    bool gnu_debug::operator==(const _Safe_iterator<gnu_cxx::
    normal_iterator<int*, std::vector<int, std::allocator<int> > >, std::
    debug::vector<int>, std::forward_iterator_tag>::_Self&, const
    _Safe_iterator<gnu_cxx::normal_iterator<int*, std::vector<int,
    std::allocator<int> > >, std::debug::vector<int>,
    std::forward_iterator_tag>::_Self&)

Error: attempt to compare a dereferenceable (start-of-sequence) iterator to
a singular (value-initialized) iterator.

Objects involved in the operation:
    iterator "lhs" @ 0x7ffe65c66710 {
      type = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > > (mutable iterator);
      state = dereferenceable (start-of-sequence);
      references sequence with type 'std::debug::vector<int,
std::allocator<int> >' @ 0x7ffe65c66740
    }
    iterator "rhs" @ 0x7ffe65c666e0 {
      type = gnu_cxx::normal_iterator<int*, std::vector<int,
std::allocator<int> > > (mutable iterator);
      state = singular (value-initialized);
    }
Aborted                    (core dumped) ./a.out


>
> (streambuf_iterator uses the default-initialized state as its sentinel
> value, so that avenue is closed for it.)
>
> Iterators are normally passed and returned by value, so a distinct
> moved-from state lost on copying would be lost very quickly.
> Why couldn't the moved-from state be carried across copies?
> I think we would also like to trap comparing the copies.

We do today, but the way we do it leads to false positives, see the
examples in PR 127540.

I think we'd get what you want if we just remove the assertions in the
copy and move operations. That would allow copying singular iterators,
including moved-from iterators, but the originals and the copies would
remain radioactive and not comparable.



More information about the Libstdc++ mailing list