Hi
Here is what I've come up with so far.
This is clearly not limited to fix of PR 127540 but it does fix it.
This is a big performance improvement for the _GLIBCXX_DEBUG mode. The
proof is that I managed to run testsuite/23_containers/flat_* tests.
They were all timeout-ing on my laptop without the patch.
I still need to work on the ChangeLog entry which I copied from your RFC
Jonathan.
I've also prepared the same for _Safe_local_iterator.
Happy to have some feedback.
libstdc++: Fix _Safe_iterator copy/move semantic PR 127540
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
* config/abi/pre/gnu.ver (GLIBCXX_3.4.37): Add
_Safe_sequence_base::_M_attach
and _Safe_sequence_base::_M_attach_single symbols so that
freshly instantiated
safe iterators can be attached without going through a
detach attempt.
* include/debug/safe_base.h
(_Safe_iterator_base): Remove implicit C++20 constexpr
qualification.
(_Safe_iterator_base::operator=(const
_Safe_iterator_base&)): New, default.
(_Safe_iterator_base::_M_get_sequence): New, get sequence
pointer if not singular.
(_Safe_iterator_base::_M_attach_only): New.
(_Safe_iterator_base(const _Safe_sequence_base*, bool)):
Use latter.
(_Safe_iterator_base(const _Safe_iterator_base&, bool)):
Likewise and only copy
input _Safe_iterator_base _M_sequence and _M_version if
singular.
* include/debug/safe_iterator.h:
(_Safe_iterator(const _Safe_iterator&)): Forward to latter
_Safe_iterator_base
constructor.
(_Safe_iterator<_MutableIte>(const _Safe_iterator<>&)):
Likewise.
(_Safe_iterator): Add friend declaration to access mutable
iterator members
from the immutable type.
(_Safe_iterator::_M_move): New, make moved instance
value-initialized.
(_Safe_iterator(_Safe_iterator&&, bool)): New.
(_Safe_iterator(_Safe_iterator&&)): Forward to latter.
(_Safe_iterator<_MutableIte>(_Safe_iterator<_MutableIte, _Seq, _Cat>&&)):
Likewise.
(_Safe_iterator::_M_copy_assign<_Ite>): New.
(_Safe_iterator::operator=(const _Safe_iterator&)): Call
latter.
(_Safe_iterator::operator=<_MutableIte>(const
_Safe_iterator<>&)): New, likewise.
(_Safe_iterator::_M_move_assign): New.
(_Safe_iterator::operator=(_Safe_iterator&&)): Call latter.
(_Safe_iterator::operator=<_MutableIte>(_Safe_iterator<>&&)): New, likewise.
(_Safe_iterator::_M_attach): Remove.
(_Safe_iterator::_M_attach_single): Remove.
* src/c++11/debug.cc (_Safe_iterator_base::_M_attach): Do
not reset _M_version
if attaching to a null sequence.
(_Safe_iterator_base::_M_attach_single): Likewise.
* testsuite/24_iterators/debug/safe_iterator.cc: New test.
Tested under Linux x86_64 23_containers and 24_iterators tests.
François
diff --git a/libstdc++-v3/config/abi/pre/gnu.ver
b/libstdc++-v3/config/abi/pre/gnu.ver
index 4fbb87acee3..ef040ca4056 100644
--- a/libstdc++-v3/config/abi/pre/gnu.ver
+++ b/libstdc++-v3/config/abi/pre/gnu.ver
@@ -2641,6 +2641,8 @@ GLIBCXX_3.4.37 {
_ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE*_S_allocate_*;
_ZNSt7__cxx1112basic_stringI[cw]St11char_traitsI[cw]ESaI[cw]EE*_M_create_*;
+
_ZNK11__gnu_debug19_Safe_sequence_base9_M_attachEPNS_19_Safe_iterator_baseEb;
+
_ZNK11__gnu_debug19_Safe_sequence_base16_M_attach_singleEPNS_19_Safe_iterator_baseEb;
} GLIBCXX_3.4.36;
# Symbols in the support library (libsupc++) have their own tag.
diff --git a/libstdc++-v3/include/debug/safe_base.h
b/libstdc++-v3/include/debug/safe_base.h
index 776b86ee701..c6946d4ed0e 100644
--- a/libstdc++-v3/include/debug/safe_base.h
+++ b/libstdc++-v3/include/debug/safe_base.h
@@ -79,9 +79,12 @@ namespace __gnu_debug
protected:
/** Initializes the iterator and makes it singular. */
- _GLIBCXX20_CONSTEXPR
_Safe_iterator_base() = default;
+ _Safe_iterator_base&
+ operator=(const _Safe_iterator_base&) = default;
+#pragma GCC diagnostic pop
+
/** Initialize the iterator to reference the sequence pointed to
* by @p __seq. @p __constant is true when we are initializing a
* constant iterator, and false if it is a mutable iterator. Note
@@ -90,24 +93,13 @@ namespace __gnu_debug
* be nonsingular.
*/
_GLIBCXX20_CONSTEXPR
- _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
- : _Safe_iterator_base()
- {
- if (!std::__is_constant_evaluated())
- this->_M_attach(__seq, __constant);
- }
+ _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant);
/** Initializes the iterator to reference the same sequence that
@p __x does. @p __constant is true if this is a constant
iterator, and false if it is mutable. */
_GLIBCXX20_CONSTEXPR
- _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);
- }
-#pragma GCC diagnostic pop
+ _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant);
_GLIBCXX20_CONSTEXPR
~_Safe_iterator_base()
@@ -116,6 +108,15 @@ namespace __gnu_debug
this->_M_detach();
}
+ const _Safe_sequence_base*
+ _M_get_sequence() const _GLIBCXX_NOEXCEPT
+ {
+ if (_M_singular())
+ return 0;
+
+ return _M_sequence;
+ }
+
/** For use in _Safe_iterator. */
__gnu_cxx::__mutex&
_M_get_mutex() _GLIBCXX_USE_NOEXCEPT;
@@ -133,6 +134,17 @@ namespace __gnu_debug
_M_attach_single(const _Safe_sequence_base* __seq,
bool __constant) _GLIBCXX_USE_NOEXCEPT;
+ /** Like _M_attach but do not try to detach. */
+ void
+ _M_attach_only(const _Safe_sequence_base* __seq,
+ bool __constant) _GLIBCXX_NOEXCEPT;
+
+ /** Like _M_attach_only but not thread-safe. */
+ void
+ _M_attach_only(const __gnu_cxx::__scoped_lock&,
+ const _Safe_sequence_base* __seq,
+ bool __constant) _GLIBCXX_NOEXCEPT;
+
/** Detach the iterator from whatever sequence it is attached to,
* if any.
*/
@@ -354,6 +366,60 @@ namespace __gnu_debug
void
_M_detach_single(_Safe_iterator_base* __it) const _GLIBCXX_USE_NOEXCEPT;
};
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wc++11-extensions"
+ _GLIBCXX20_CONSTEXPR
+ _Safe_iterator_base::
+ _Safe_iterator_base(const _Safe_sequence_base* __seq, bool __constant)
+ : _Safe_iterator_base()
+ {
+ if (!std::__is_constant_evaluated())
+ {
+ if (__seq)
+ _M_attach_only(__seq, __constant);
+ }
+ }
+
+ _GLIBCXX20_CONSTEXPR
+ _Safe_iterator_base::
+ _Safe_iterator_base(const _Safe_iterator_base& __x, bool __constant)
+ : _Safe_iterator_base()
+ {
+ if (!std::__is_constant_evaluated())
+ {
+ const _Safe_sequence_base* __seq = __x._M_get_sequence();
+ if (__seq)
+ _M_attach_only(__seq, __constant);
+ else
+ {
+ // Copy __x so that we preserve the value-initialized state of
+ // __x on the new instance and have it comparable.
+ _M_sequence = __x._M_sequence;
+ _M_version = __x._M_version;
+ }
+ }
+ }
+#pragma GCC diagnostic pop
+
+ inline void
+ _Safe_iterator_base::_M_attach_only(const _Safe_sequence_base* __seq,
+ bool __constant) _GLIBCXX_NOEXCEPT
+ {
+ _M_sequence = __seq;
+ _M_version = __seq->_M_version;
+ __seq->_M_attach(this, __constant);
+ }
+
+ inline void
+ _Safe_iterator_base::_M_attach_only(const __gnu_cxx::__scoped_lock&,
+ const _Safe_sequence_base* __seq,
+ bool __constant) _GLIBCXX_NOEXCEPT
+ {
+ _M_sequence = __seq;
+ _M_version = __seq->_M_version;
+ __seq->_M_attach_single(this, __constant);
+ }
} // namespace __gnu_debug
#endif
diff --git a/libstdc++-v3/include/debug/safe_iterator.h
b/libstdc++-v3/include/debug/safe_iterator.h
index 8c049961cca..277a72ec669 100644
--- a/libstdc++-v3/include/debug/safe_iterator.h
+++ b/libstdc++-v3/include/debug/safe_iterator.h
@@ -133,6 +133,10 @@ namespace __gnu_debug
{
typedef std::iterator_traits<_Iterator> _Traits;
+ // To access mutable iterator members from the immutable one.
+ 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 +165,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,50 +187,103 @@ 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:
+ /**
+ * @post this is value-initialized so that it is still usable for
+ * comparison.
+ */
+ _Iterator
+ _M_move() 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);
+ static const _Safe_iterator __value_init_it{};
+
+ _Iterator __it = *this;
+ this->_Safe_iterator_base::operator=(__value_init_it);
+ base() = __value_init_it.base();
+ return __it;
}
-#if __cplusplus >= 201103L
+ template<typename _Ite>
+ _GLIBCXX20_CONSTEXPR
+ _Safe_iterator(_Safe_iterator<_Ite, _Sequence, _Category>&& __x,
+ bool __x_constant) noexcept
+ : _Iterator(), _Safe_iterator_base()
+ {
+ if (std::__is_constant_evaluated())
+ {
+ base() = __x.base();
+ return;
+ }
+ else
+ {
+ auto __seq = __x._M_get_sequence();
+ if (!__seq)
+ {
+ // Copy __x values so that we preserve the
+ // value-initialized state of __x on the new instance.
+ _M_version = __x._M_version;
+ _M_sequence = __x._M_sequence;
+ base() = __x.base();
+ }
+ else
+ {
+ _M_sequence = __seq;
+ _M_version = __seq->_M_version;
+
+ // Use base class reference to avoid ambiguity with potential
+ // _Iterator members.
+ _Safe_iterator_base& __this_base = *this;
+ _Safe_iterator_base& __x_base = __x;
+ __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
+ if (_S_constant() == __x_constant)
+ {
+ auto __prior = __x_base._M_prior;
+ if (__prior)
+ {
+ __prior->_M_next = this;
+ __this_base._M_prior = __prior;
+ }
+
+ auto __next = __x_base._M_next;
+ if (__next)
+ {
+ __next->_M_prior = this;
+ __this_base._M_next = __next;
+ }
+
+ if (__seq->_M_const_iterators == std::addressof(__x_base))
+ __seq->_M_const_iterators = this;
+ else if (__seq->_M_iterators == std::addressof(__x_base))
+ __seq->_M_iterators = this;
+ }
+ else
+ {
+ __x._M_detach_single();
+ this->_M_attach_only(__l, __seq, _S_constant());
+ }
+
+ base() = __x._M_move();
+ }
+ }
+ }
+
+ 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>
@@ -237,57 +294,133 @@ namespace __gnu_debug
std::__are_same<_MutableIterator, _OtherIterator>::__value,
_Category>::__type>& __x)
_GLIBCXX_NOEXCEPT
- : _Iterator(__x.base())
+ : _Iterator(__x.base()), _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
{
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;
+ }
+ else
+ {
+ const _Safe_sequence_base* __x_seq = __x._M_get_sequence();
+ const _Safe_sequence_base* __seq = this->_M_get_sequence();
+ if (__seq && __seq == __x_seq)
+ {
+ __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
+ base() = __x.base();
+ }
+ else
+ {
+ _M_detach();
+ if (__x_seq)
+ {
+ base() = __x.base();
+ this->_M_attach_only(__x_seq, _S_constant());
+ }
+ else
+ {
+ // Copy __x so that we preserve its value-initialized
+ // state.
+ _M_version = __x._M_version;
+ _M_sequence = __x._M_sequence;
+ base() = __x.base();
+ }
+ }
+
+ 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;
+ }
+ else
+ {
+ _Safe_iterator_base& __x_base = __x;
+ _Safe_iterator_base& __this_base = *this;
+ if (std::addressof(__x_base) == std::addressof(__this_base))
+ 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)
+ {
+ __gnu_cxx::__scoped_lock __l(this->_M_get_mutex());
+ __x._M_detach_single();
+ base() = __x._M_move();
+ }
+ else
+ {
+ _M_detach();
+
+ if (__x_seq)
+ {
+ __x._M_detach();
+ base() = __x._M_move();
+ this->_M_attach_only(__x_seq, _S_constant());
+ }
+ else
+ {
+ // Copy __x so that we preserve its value-initialized
+ // state.
+ _M_version = __x._M_version;
+ base() = __x.base();
+ }
+ }
+ }
+
+ 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;
- }
+ { return _M_copy_assign(__x); }
- // _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 *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 +430,16 @@ namespace __gnu_debug
_GLIBCXX20_CONSTEXPR
_Safe_iterator&
operator=(_Safe_iterator&& __x) noexcept
- {
- if (std::__is_constant_evaluated())
- {
- base() = __x.base();
- return *this;
- }
-
- _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;
+ { return _M_move_assign(std::move(__x)); }
- 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
/**
@@ -437,16 +547,6 @@ namespace __gnu_debug
_GLIBCXX20_CONSTEXPR
operator _Iterator() const _GLIBCXX_NOEXCEPT { return *this; }
- /** Attach iterator to the given sequence. */
- void
- _M_attach(const _Safe_sequence_base* __seq)
- { _Safe_iterator_base::_M_attach(__seq, _S_constant()); }
-
- /** Likewise, but not thread-safe. */
- void
- _M_attach_single(const _Safe_sequence_base* __seq)
- { _Safe_iterator_base::_M_attach_single(__seq, _S_constant()); }
-
/// Is the iterator dereferenceable?
bool
_M_dereferenceable() const
@@ -490,16 +590,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 +712,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 +721,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 +750,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 +757,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 +768,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 +906,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 +918,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 +946,70 @@ 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 &&
+ 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?
diff --git a/libstdc++-v3/src/c++11/debug.cc b/libstdc++-v3/src/c++11/debug.cc
index 9a36bdfabb9..127cf7312d9 100644
--- a/libstdc++-v3/src/c++11/debug.cc
+++ b/libstdc++-v3/src/c++11/debug.cc
@@ -478,8 +478,6 @@ namespace __gnu_debug
_M_version = _M_sequence->_M_version;
_M_sequence->_M_attach(this, __constant);
}
- else
- _M_version = 0;
}
void
@@ -495,8 +493,6 @@ namespace __gnu_debug
_M_version = _M_sequence->_M_version;
_M_sequence->_M_attach_single(this, __constant);
}
- else
- _M_version = 0;
}
void
diff --git a/libstdc++-v3/testsuite/24_iterators/debug/safe_iterator.cc
b/libstdc++-v3/testsuite/24_iterators/debug/safe_iterator.cc
new file mode 100644
index 00000000000..beca21740e7
--- /dev/null
+++ b/libstdc++-v3/testsuite/24_iterators/debug/safe_iterator.cc
@@ -0,0 +1,83 @@
+// { dg-options "-D_GLIBCXX_DEBUG" }
+// { dg-do run { target c++11 } }
+
+#include <vector>
+#include <algorithm>
+
+#include <testsuite_hooks.h>
+
+static void
+test_swap()
+{
+ std::vector<int> v{1};
+ auto i = v.begin();
+ std::swap(i, i);
+ VERIFY( *i == 1 );
+}
+
+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
+ VERIFY( *j == 1 );
+ j = i; // copy assignment using moved-from iterator
+ VERIFY( j == i );
+ auto k = i; // copy construction using moved-from iterator
+ VERIFY( k == i );
+ k = i; // copy assignment using moved-from iterator
+ VERIFY( k == i );
+
+ i = v.begin();
+ j = std::move(i); // i enters moved-from state via move assignment
+ VERIFY( *j == 1 );
+ j = i; // copy assignment using moved-from iterator
+ VERIFY( j == i );
+ auto l = i; // copy construction using moved-from iterator
+ VERIFY( l == i );
+ l = i; // copy assignment using moved-from iterator
+ VERIFY( l == i );
+}
+
+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
+}
+
+static void
+test_conversion()
+{
+ std::vector<int> v{1};
+ auto i = v.begin();
+ typename std::vector<int>::const_iterator j = std::move(i); // i enters
moved-from state via move constructor
+ j = std::move(i); // move assignment using moved-from iterator
+ typename std::vector<int>::const_iterator 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
+ typename std::vector<int>::const_iterator 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();
+ test_conversion();
+}