[RFC] libstdc++: Leave safe iterators in valid state post-move [PR127540]
Nathan Myers
ncm@cantrip.org
Fri Oct 16 14:36:40 GMT 2026
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.
(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.
> If we care about diagnosing invalid comparisons, we would need to
> introduce a new moved-from state to the iterators. That could then be
> checked in the iterator's constructors and assignment operators so that
> moved-from iterators are allowed as the source of those operations. The
> new state would not be checked in comparisons so that using them would
> abort, as was the case before this commit, e.g.
>
> /home/jwakely/gcc/17/include/c++/17.0.0/debug/safe_iterator.h:546:
> In function:
> constexpr 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 singular iterator to a dereferenceable iterator.
>
> libstdc++-v3/ChangeLog:
>
> PR libstdc++/127540
> * include/debug/safe_iterator.h (_Safe_iterator): Set _M_version
> to zero after move.
> * testsuite/24_iterators/debug/127540.cc: New test.
> ---
>
> Tested x86_64-linux.
>
> Posting this as an RFC because it's not the only solution.
>
> In bugzilla François suggested an alternative solution that would leave
> moved-from iterators in the same state they are now (singular and
> without clearing _M_version) but copying them would produce a
> value-initialized iterator. I haven't tested that, but I think it would
> mean moved-from iterators would abort if used in equality comparisons
> (but copies of them would not). That might be preferable, although the
> "moved-from means value-initialized" model is simpler to explain.
>
> libstdc++-v3/include/debug/safe_iterator.h | 7 +--
> .../testsuite/24_iterators/debug/127540.cc | 54 +++++++++++++++++++
> 2 files changed, 56 insertions(+), 5 deletions(-)
> create mode 100644 libstdc++-v3/testsuite/24_iterators/debug/127540.cc
>
> diff --git a/libstdc++-v3/include/debug/safe_iterator.h b/libstdc++-v3/include/debug/safe_iterator.h
> index 8c049961cca1..6a5b8fd61fca 100644
> --- a/libstdc++-v3/include/debug/safe_iterator.h
> +++ b/libstdc++-v3/include/debug/safe_iterator.h
> @@ -213,15 +213,11 @@ namespace __gnu_debug
> return;
> }
>
> - _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
> - || __x._M_value_initialized(),
> - _M_message(__msg_init_copy_singular)
> - ._M_iterator(*this, "this")
> - ._M_iterator(__x, "other"));
> const _Safe_sequence_base* __seq = __x._M_sequence;
> __x._M_detach();
> std::swap(base(), __x.base());
> _M_attach(__seq);
> + __x._M_version = 0;
> }
> #endif
>
> @@ -328,6 +324,7 @@ namespace __gnu_debug
>
> __x._M_detach();
> __x.base() = _Iterator();
> + __x._M_version = 0;
> return *this;
> }
> #endif
> diff --git a/libstdc++-v3/testsuite/24_iterators/debug/127540.cc b/libstdc++-v3/testsuite/24_iterators/debug/127540.cc
> new file mode 100644
> index 000000000000..79c6bab67d02
> --- /dev/null
> +++ b/libstdc++-v3/testsuite/24_iterators/debug/127540.cc
> @@ -0,0 +1,54 @@
> +// { dg-options "-D_GLIBCXX_DEBUG" }
> +// { dg-do run { target c++11 } }
> +
> +#include <vector>
> +#include <algorithm>
> +
> +static void
> +test_swap()
> +{
> + std::vector<int> v{1};
> + auto i = v.begin();
> + std::swap(i, i);
> +}
> +
> +static void
> +test_copy()
> +{
> + std::vector<int> v{1};
> + auto i = v.begin();
> + auto j = std::move(i); // i enters moved-from state via move constructor
> + j = i; // copy assignment using moved-from iterator
> + auto k = i; // copy construction using moved-from iterator
> + k = i; // copy assignment using moved-from iterator
> +
> + i = v.begin();
> + j = std::move(i); // i enters moved-from state via move assignment
> + j = i; // copy assignment using moved-from iterator
> + auto l = i; // copy construction using moved-from iterator
> + l = i; // copy assignment using moved-from iterator
> +}
> +
> +static void
> +test_move()
> +{
> + std::vector<int> v{1};
> + auto i = v.begin();
> + auto j = std::move(i); // i enters moved-from state via move constructor
> + j = std::move(i); // move assignment using moved-from iterator
> + auto k = std::move(i); // move construction using moved-from iterator
> + k = std::move(i); // move assignment using moved-from iterator
> +
> + i = v.begin();
> + j = std::move(i); // i enters moved-from state via move assignment
> + j = std::move(i); // move assignment using moved-from iterator
> + auto l = std::move(i); // move construction using moved-from iterator
> + l = std::move(i); // move assignment using moved-from iterator
> +}
> +
> +int main()
> +{
> + test_swap();
> + test_copy();
> + test_move();
> +}
More information about the Libstdc++
mailing list