IMHO I think it is the right fix to do for this PR.

On my side this PR made me review the copy/move constructors and assignment operators of the _Safe_iterator.

For information I'm attaching what I've done so far. There is still some work to do on _Safe_iterator and then _Safe_local_iterator to rework too.

If this RFC was to be committed I would advise to rename the test file to something else than the PR number. It is testing more than that and I plan to add more test case to it.

François

On 10/16/26 12:16, 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).

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();
+}
diff --git a/libstdc++-v3/include/debug/safe_base.h 
b/libstdc++-v3/include/debug/safe_base.h
index 776b86ee701..a35d768d001 100644
--- a/libstdc++-v3/include/debug/safe_base.h
+++ b/libstdc++-v3/include/debug/safe_base.h
@@ -104,8 +104,19 @@ namespace __gnu_debug
     _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
     : _Safe_iterator_base()
     {
-      if (!std::__is_constant_evaluated())
-       this->_M_attach(__x._M_sequence, __constant);
+      if (std::__is_constant_evaluated())
+       return;
+
+      const _Safe_sequence_base* __x_seq = __x._M_get_sequence();
+      if (__x_seq)
+       _M_attach(__x_seq, __constant);
+      else
+       {
+         // Copy __x so that we preserve the value-initialized state of __x
+         // on the new instance.
+         _M_sequence = __x._M_sequence;
+         _M_version = __x._M_version;
+       }
     }
 #pragma GCC diagnostic pop
 
@@ -116,6 +127,10 @@ namespace __gnu_debug
        this->_M_detach();
     }
 
+    _GLIBCXX20_CONSTEXPR
+    const _Safe_sequence_base*
+    _M_get_sequence() const _GLIBCXX_NOEXCEPT;
+
     /** For use in _Safe_iterator. */
     __gnu_cxx::__mutex&
     _M_get_mutex() _GLIBCXX_USE_NOEXCEPT;
@@ -354,6 +369,16 @@ namespace __gnu_debug
     void
     _M_detach_single(_Safe_iterator_base* __it) const _GLIBCXX_USE_NOEXCEPT;
   };
+
+  _GLIBCXX20_CONSTEXPR
+  inline const _Safe_sequence_base*
+  _Safe_iterator_base::_M_get_sequence() const _GLIBCXX_NOEXCEPT
+  {
+    if (!_M_sequence || _M_sequence->_M_version != _M_version)
+      return 0;
+
+    return _M_sequence;
+  }
 } // namespace __gnu_debug
 
 #endif
diff --git a/libstdc++-v3/include/debug/safe_iterator.h 
b/libstdc++-v3/include/debug/safe_iterator.h
index 8c049961cca..c5b8f57751d 100644
--- a/libstdc++-v3/include/debug/safe_iterator.h
+++ b/libstdc++-v3/include/debug/safe_iterator.h
@@ -133,6 +133,9 @@ namespace __gnu_debug
     {
       typedef std::iterator_traits<_Iterator> _Traits;
 
+      template<typename _Ite, typename _Seq, typename _Cat>
+       friend class _Safe_iterator;
+
     protected:
       typedef std::__are_same<typename _Sequence::_Base::const_iterator,
                              _Iterator> _IsConstant;
@@ -161,7 +164,7 @@ namespace __gnu_debug
       using iterator_concept = std::__detail::__iter_concept<_Iterator>;
 #endif
 
-      /// @post the iterator is singular and unattached
+      /// @post the iterator is singular, value-initialized and unattached
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator() _GLIBCXX_NOEXCEPT : _Iterator() { }
 
@@ -183,111 +186,233 @@ namespace __gnu_debug
        */
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
-      : _Iterator(__x), _Safe_iterator_base()
+      : _Iterator(__x), _Safe_iterator_base(__x, _S_constant())
+      { }
+
+#if __cplusplus >= 201103L
+    private:
+      _GLIBCXX20_CONSTEXPR void
+      _M_reset() noexcept
       {
-       if (std::__is_constant_evaluated())
-         return;
-
-       // _GLIBCXX_RESOLVE_LIB_DEFECTS
-       // DR 408. Is vector<reverse_iterator<char*> > forbidden?
-       _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
-                             || __x._M_value_initialized(),
-                             _M_message(__msg_init_copy_singular)
-                             ._M_iterator(*this, "this")
-                             ._M_iterator(__x, "other"));
-       _M_attach(__x._M_sequence);
+       this->_M_sequence = nullptr;
+       this->_M_version = 0;
+       base() = _Iterator{};
       }
 
-#if __cplusplus >= 201103L
+      template<typename _Ite>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator(_Safe_iterator<_Ite, _Sequence, _Category>&& __x,
+                      bool __constant) noexcept
+       {
+         if (std::__is_constant_evaluated())
+           {
+             base() = __x.base();
+             return;
+           }
+
+         auto __seq = __x._M_get_sequence();
+         if (__seq)
+           {
+             _M_sequence = __seq;
+             _M_version = __x._M_version;
+             _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_BEGIN {
+               __gnu_cxx::__scoped_lock __l(__x._M_get_mutex());
+               if (_S_constant() == __constant)
+                 {
+                   if (__x._M_prior)
+                     {
+                       __x._M_prior->_M_next = this;
+                       _M_prior = __x._M_prior;
+                     }
+                   else
+                     {
+                       if (__constant)
+                         __seq->_M_const_iterators = this;
+                       else
+                         __seq->_M_iterators = this;
+                     }
+
+                   if (__x._M_next)
+                     {
+                       __x._M_next->_M_prior = this;
+                       _M_next = __x._M_next;
+                     }
+                 }
+               else
+                 {
+                   __x._M_detach_single();
+                   _M_attach_single(__seq);
+                 }
+
+               base() = __x.base();
+             } _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_END
+
+             // Make __x value-initialized so that it is still usable for
+             // comparison.
+             __x._M_reset();
+           }
+         else
+           {
+             // Copy __x so that we preserve its value-initialized state and 
its
+             // originally attached sequence in case of assertion.
+             _M_sequence = __x._M_sequence;
+             _M_version = __x._M_version;
+           }
+       }
+
+    public:
       /**
        * @brief Move construction.
-       * @post __x is singular and unattached
+       * @post __x is singular, value-initialized and unattached
        */
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator(_Safe_iterator&& __x) noexcept
-      : _Iterator()
-      {
-       if (std::__is_constant_evaluated())
-         {
-           base() = __x.base();
-           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);
-      }
+      : _Safe_iterator(std::move(__x), _S_constant())
+      { }
 #endif
 
       /**
-       *  @brief Converting constructor from a mutable iterator to a
+       *  @brief Converting copy constructor from a mutable iterator to a
        *  constant iterator.
       */
       template<typename _MutableIterator>
        _GLIBCXX20_CONSTEXPR
-       _Safe_iterator(
-         const _Safe_iterator<_MutableIterator, _Sequence,
+       _Safe_iterator(const _Safe_iterator<_MutableIterator, _Sequence,
            typename __gnu_cxx::__enable_if<_IsConstant::__value &&
              std::__are_same<_MutableIterator, _OtherIterator>::__value,
-                              _Category>::__type>& __x)
+                              _Category>::__type>& __x) _GLIBCXX_NOEXCEPT
+       : _Iterator(__x), _Safe_iterator_base(__x, _S_constant()) { }
+
+#if __cplusplus >= 201103L
+      /**
+       *  @brief Converting move constructor from a mutable iterator to a
+       *  constant iterator.
+      */
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator(_Safe_iterator<_MutableIterator, _Sequence,
+           typename std::__enable_if_t<_IsConstant::__value &&
+             std::is_same<_MutableIterator, _OtherIterator>::value,
+                                   _Category>>&& __x) noexcept
+       : _Safe_iterator(std::move(__x), false) { }
+#endif
+
+    private:
+      template<typename _Ite>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       _M_copy_assign(const _Safe_iterator<_Ite, _Sequence, _Category>& __x)
        _GLIBCXX_NOEXCEPT
-       : _Iterator(__x.base())
        {
          if (std::__is_constant_evaluated())
-           return;
-
-         // _GLIBCXX_RESOLVE_LIB_DEFECTS
-         // DR 408. Is vector<reverse_iterator<char*> > forbidden?
-         _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
-                               || __x._M_value_initialized(),
-                               _M_message(__msg_init_const_singular)
-                               ._M_iterator(*this, "this")
-                               ._M_iterator(__x, "other"));
-         _M_attach(__x._M_sequence);
+           {
+             base() = __x.base();
+             return *this;
+           }
+
+         const _Safe_sequence_base* __x_seq = __x._M_get_sequence();
+         const _Safe_sequence_base* __seq = this->_M_get_sequence();
+         if (__seq && __seq == __x_seq)
+           _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_BEGIN {
+             __gnu_cxx::__scoped_lock __l(_M_get_mutex());
+             base() = __x.base();
+           } _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_END
+         else
+           {
+             _M_detach();
+             if (__x_seq)
+               {
+                 base() = __x.base();
+                 _M_attach(__x_seq);
+               }
+             else
+               {
+                 // Copy __x so that we preserve its value-initialized state
+                 // and its originally attached sequence in case of assertion.
+                 _M_sequence = __x._M_sequence;
+                 _M_version = __x._M_version;
+               }
+           }
+
+         return *this;
        }
 
+
+#if __cplusplus >= 201103L
+      template<typename _Ite>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       _M_move_assign(_Safe_iterator<_Ite, _Sequence, _Category>&& __x)
+         noexcept
+       {
+         if (std::__is_constant_evaluated())
+           {
+             base() = __x.base();
+             return *this;
+           }
+
+         if (std::__addressof(__x) == this)
+           return *this;
+
+         const _Safe_sequence_base* __x_seq = __x._M_get_sequence();
+         const _Safe_sequence_base* __seq = _M_get_sequence();
+         if (__seq && __seq == __x_seq)
+           {
+             _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_BEGIN {
+               __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
+               __x._M_detach_single();
+               base() = __x.base();
+             } _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_END
+
+             // Make __x value-initialized so that it is still usable for
+             // comparison.
+             __x._M_reset();
+           }
+         else
+           {
+             _M_detach();
+
+             if (__x_seq)
+               {
+                 __x._M_detach();
+                 base() = __x.base();
+
+                 _M_attach(__x_seq);
+
+                 // Make __x value-initialized so that it is still usable for
+                 // comparison.
+                 __x._M_reset();
+               }
+             else
+               {
+                 // Copy the version so that we preserve the value-initialized
+                 // state of __x on the new singular instance.
+                 _M_sequence = __x._M_sequence;
+                 _M_version = __x._M_version;
+               }
+           }
+
+         return *this;
+       }
+#endif
+
+    public:
       /**
        * @brief Copy assignment.
        */
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator&
       operator=(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
-      {
-       if (std::__is_constant_evaluated())
-         {
-           base() = __x.base();
-           return *this;
-         }
-
-       // _GLIBCXX_RESOLVE_LIB_DEFECTS
-       // DR 408. Is vector<reverse_iterator<char*> > forbidden?
-       _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
-                             || __x._M_value_initialized(),
-                             _M_message(__msg_copy_singular)
-                             ._M_iterator(*this, "this")
-                             ._M_iterator(__x, "other"));
-
-       if (this->_M_sequence && this->_M_sequence == __x._M_sequence)
-         _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_BEGIN {
-           __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
-           base() = __x.base();
-           _M_version = __x._M_sequence->_M_version;
-         } _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_END
-       else
-         {
-           _M_detach();
-           base() = __x.base();
-           _M_attach(__x._M_sequence);
-         }
+      { return _M_copy_assign(__x); }
 
-       return *this;
-      }
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       operator=(const _Safe_iterator<_MutableIterator, _Sequence,
+                 typename __gnu_cxx::__enable_if<_IsConstant::__value &&
+                   std::__are_same<_MutableIterator, _OtherIterator>::__value,
+                               _Category>::__type>& __x) _GLIBCXX_NOEXCEPT
+       { return _M_copy_assign(__x); }
 
 #if __cplusplus >= 201103L
       /**
@@ -297,39 +422,16 @@ namespace __gnu_debug
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator&
       operator=(_Safe_iterator&& __x) noexcept
-      {
-       if (std::__is_constant_evaluated())
-         {
-           base() = __x.base();
-           return *this;
-         }
+      { return _M_move_assign(std::move(__x)); }
 
-       _GLIBCXX_DEBUG_VERIFY(!__x._M_singular()
-                             || __x._M_value_initialized(),
-                             _M_message(__msg_copy_singular)
-                             ._M_iterator(*this, "this")
-                             ._M_iterator(__x, "other"));
-
-       if (std::__addressof(__x) == this)
-         return *this;
-
-       if (this->_M_sequence && this->_M_sequence == __x._M_sequence)
-         _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_BEGIN {
-           __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
-           base() = __x.base();
-           _M_version = __x._M_sequence->_M_version;
-         } _GLIBCXX20_CONSTEXPR_NON_LITERAL_SCOPE_END
-       else
-         {
-           _M_detach();
-           base() = __x.base();
-           _M_attach(__x._M_sequence);
-         }
-
-       __x._M_detach();
-       __x.base() = _Iterator();
-       return *this;
-      }
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       operator=(_Safe_iterator<_MutableIterator, _Sequence,
+                 typename std::__enable_if_t<_IsConstant::__value &&
+                   std::is_same<_MutableIterator, _OtherIterator>::value,
+                                _Category>>&& __x) noexcept
+       { return _M_move_assign(std::move(__x)); }
 #endif
 
       /**
@@ -490,16 +592,19 @@ namespace __gnu_debug
                     std::pair<difference_type, _Distance_precision>& __dist,
                     bool __check_dereferenceable = true) const;
 
-      // The sequence this iterator references.
+      // The sequence this iterator references, if not singular.
+      _GLIBCXX20_CONSTEXPR
       typename __gnu_cxx::__conditional_type<
        _IsConstant::__value, const _Sequence*, _Sequence*>::__type
-      _M_get_sequence() const
+      _M_get_sequence() const _GLIBCXX_NOEXCEPT
       {
+       const _Safe_sequence_base* __seq
+         = _Safe_iterator_base::_M_get_sequence();
+
        // Looks like not const-correct, but if _IsConstant the constness
        // is restored when returning the sequence pointer and if not
        // _IsConstant we are allowed to remove constness.
-       return static_cast<_Sequence*>
-         (const_cast<_Safe_sequence_base*>(_M_sequence));
+       return static_cast<_Sequence*>(const_cast<_Safe_sequence_base*>(__seq));
       }
 
       // Get distance to __rhs.
@@ -609,6 +714,8 @@ namespace __gnu_debug
 
       typedef typename _Safe_base::_Unchecked _Unchecked;
 
+      typedef typename _Safe_base::_IsConstant _IsConstant;
+
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator(const _Safe_iterator& __x,
                     _Unchecked __unchecked) _GLIBCXX_NOEXCEPT
@@ -616,9 +723,21 @@ namespace __gnu_debug
       { }
 
     public:
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wc++11-extensions"
       /// @post the iterator is singular and unattached
-      _GLIBCXX20_CONSTEXPR
-      _Safe_iterator() _GLIBCXX_NOEXCEPT { }
+      _Safe_iterator() = default;
+
+      /**
+       * @brief Copy construction.
+       */
+      _Safe_iterator(const _Safe_iterator&) = default;
+
+      /** @brief Copy assignment. */
+      _Safe_iterator&
+      operator=(const _Safe_iterator&) = default;
+#pragma GCC diagnostic pop
 
       /**
        * @brief Safe iterator construction from an unsafe iterator and
@@ -633,14 +752,6 @@ namespace __gnu_debug
       : _Safe_base(__i, __seq)
       { }
 
-      /**
-       * @brief Copy construction.
-       */
-      _GLIBCXX20_CONSTEXPR
-      _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
-      : _Safe_base(__x)
-      { }
-
 #if __cplusplus >= 201103L
       /** @brief Move construction. */
       _GLIBCXX20_CONSTEXPR
@@ -648,7 +759,7 @@ namespace __gnu_debug
 #endif
 
       /**
-       *  @brief Converting constructor from a mutable iterator to a
+       *  @brief Converting copy constructor from a mutable iterator to a
        *  constant iterator.
       */
       template<typename _MutableIterator>
@@ -659,25 +770,51 @@ namespace __gnu_debug
              std::__are_same<_MutableIterator, _OtherIterator>::__value,
                               std::bidirectional_iterator_tag>::__type>& __x)
        _GLIBCXX_NOEXCEPT
-       : _Safe_base(__x)
-        { }
+       : _Safe_base(__x) { }
 
 #if __cplusplus >= 201103L
-      /** @brief Copy assignment. */
-      _Safe_iterator&
-      operator=(const _Safe_iterator&) = default;
+      /**
+       *  @brief Converting move constructor from a mutable iterator to a
+       *  constant iterator.
+      */
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator(_Safe_iterator<_MutableIterator, _Sequence,
+         typename std::__enable_if_t<_Safe_base::_IsConstant::__value &&
+           std::is_same<_MutableIterator, _OtherIterator>::value,
+               std::bidirectional_iterator_tag>>&& __x) noexcept
+       : _Safe_base(std::move(__x)) { }
+#endif
+
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       operator=(const _Safe_iterator<_MutableIterator, _Sequence,
+                 typename __gnu_cxx::__enable_if<_IsConstant::__value &&
+                   std::__are_same<_MutableIterator, _OtherIterator>::__value,
+                 std::bidirectional_iterator_tag>::__type>& __x)
+       _GLIBCXX_NOEXCEPT
+       {
+         _Safe_base::operator=(__x);
+         return *this;
+       }
 
+#if __cplusplus >= 201103L
       /** @brief Move assignment. */
       _Safe_iterator&
       operator=(_Safe_iterator&&) = default;
-#else
-      /** @brief Copy assignment. */
-      _Safe_iterator&
-      operator=(const _Safe_iterator& __x)
-      {
-       _Safe_base::operator=(__x);
-       return *this;
-      }
+
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       operator=(_Safe_iterator<_MutableIterator, _Sequence,
+                 typename std::__enable_if_t<_IsConstant::__value &&
+                   std::is_same<_MutableIterator, _OtherIterator>::value,
+                 std::bidirectional_iterator_tag>>&& __x) noexcept
+       {
+         _Safe_base::operator=(std::move(__x));
+         return *this;
+       }
 #endif
 
       // ------ Input iterator requirements ------
@@ -771,6 +908,8 @@ namespace __gnu_debug
 
       typedef typename _Safe_base::_Unchecked _Unchecked;
 
+      typedef typename _Safe_base::_IsConstant _IsConstant;
+
       _GLIBCXX20_CONSTEXPR
       _Safe_iterator(const _Safe_iterator& __x,
                     _Unchecked __unchecked) _GLIBCXX_NOEXCEPT
@@ -781,9 +920,20 @@ namespace __gnu_debug
       typedef typename _Safe_base::difference_type     difference_type;
       typedef typename _Safe_base::reference           reference;
 
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wc++11-extensions"
       /// @post the iterator is singular and unattached
-      _GLIBCXX20_CONSTEXPR
-      _Safe_iterator() _GLIBCXX_NOEXCEPT { }
+      _Safe_iterator() = default;
+
+      /**
+       * @brief Copy construction.
+       */
+      _Safe_iterator(const _Safe_iterator& __x) = default;
+
+      /** @brief Copy assignment. */
+      _Safe_iterator&
+      operator=(const _Safe_iterator&) = default;
+#pragma GCC diagnostic pop
 
       /**
        * @brief Safe iterator construction from an unsafe iterator and
@@ -798,50 +948,69 @@ namespace __gnu_debug
       : _Safe_base(__i, __seq)
       { }
 
-      /**
-       * @brief Copy construction.
-       */
-      _GLIBCXX20_CONSTEXPR
-      _Safe_iterator(const _Safe_iterator& __x) _GLIBCXX_NOEXCEPT
-      : _Safe_base(__x)
-      { }
-
 #if __cplusplus >= 201103L
       /** @brief Move construction. */
       _Safe_iterator(_Safe_iterator&&) = default;
 #endif
 
       /**
-       *  @brief Converting constructor from a mutable iterator to a
+       *  @brief Converting copy constructor from a mutable iterator to a
        *  constant iterator.
       */
       template<typename _MutableIterator>
        _GLIBCXX20_CONSTEXPR
-       _Safe_iterator(
-         const _Safe_iterator<_MutableIterator, _Sequence,
-           typename __gnu_cxx::__enable_if<_Safe_base::_IsConstant::__value &&
+       _Safe_iterator(const _Safe_iterator<_MutableIterator, _Sequence,
+           typename __gnu_cxx::__enable_if<_IsConstant::__value &&
              std::__are_same<_MutableIterator, _OtherIterator>::__value,
                               std::random_access_iterator_tag>::__type>& __x)
        _GLIBCXX_NOEXCEPT
        : _Safe_base(__x)
-        { }
+       { }
 
 #if __cplusplus >= 201103L
-      /** @brief Copy assignment. */
-      _Safe_iterator&
-      operator=(const _Safe_iterator&) = default;
+      /**
+       *  @brief Converting move constructor from a mutable iterator to a
+       *  constant iterator.
+      */
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator(_Safe_iterator<_MutableIterator, _Sequence,
+         typename std::__enable_if_t<_IsConstant::__value &&
+           std::is_same<_MutableIterator, _OtherIterator>::value,
+                       std::random_access_iterator_tag>>&& __x) noexcept
+       : _Safe_base(std::move(__x))
+       { }
+#endif
 
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       operator=(const _Safe_iterator<_MutableIterator, _Sequence,
+                 typename __gnu_cxx::__enable_if<_IsConstant::__value &&
+                   std::__are_same<_MutableIterator, _OtherIterator>::__value,
+                 std::random_access_iterator_tag>::__type>& __x)
+       _GLIBCXX_NOEXCEPT
+       {
+         _Safe_base::operator=(__x);
+         return *this;
+       }
+
+#if __cplusplus >= 201103L
       /** @brief Move assignment. */
       _Safe_iterator&
       operator=(_Safe_iterator&&) = default;
-#else
-      /** @brief Copy assignment. */
-      _Safe_iterator&
-      operator=(const _Safe_iterator& __x)
-      {
-       _Safe_base::operator=(__x);
-       return *this;
-      }
+
+      template<typename _MutableIterator>
+       _GLIBCXX20_CONSTEXPR
+       _Safe_iterator&
+       operator=(_Safe_iterator<_MutableIterator, _Sequence,
+                 typename std::__enable_if_t<_IsConstant::__value &&
+                   std::is_same<_MutableIterator, _OtherIterator>::value,
+                 std::random_access_iterator_tag>>&& __x) noexcept
+       {
+         _Safe_base::operator=(std::move(__x));
+         return *this;
+       }
 #endif
 
       // Is the iterator range [*this, __rhs) valid?

Reply via email to