On 14/02/2024 20:44, Jonathan Wakely wrote:
On Wed, 14 Feb 2024 at 18:39, François Dumont <frs.dum...@gmail.com>
wrote:
libstdc++: [_GLIBCXX_DEBUG] Fix std::__niter_base behavior
std::__niter_base is used in _GLIBCXX_DEBUG mode to remove
_Safe_iterator<>
wrapper on random access iterators. But doing so it should also
preserve
original
behavior to remove __normal_iterator wrapper.
libstdc++-v3/ChangeLog:
* include/bits/stl_algobase.h (std::__niter_base): Redefine the
overload
definitions for __gnu_debug::_Safe_iterator.
* include/debug/safe_iterator.tcc (std::__niter_base): Adapt
declarations.
Ok to commit once all tests completed (still need to check
pre-c++11) ?
The declaration in include/bits/stl_algobase.h has a
noexcept-specifier but the definition in
include/debug/safe_iterator.tcc does not have one - that seems wrong
(I'm surprised it even compiles).
It does ! I thought it was only necessary at declaration, and I also had
troubles doing it right at definition because of the interaction with
the auto and ->. Now simplified and consistent in this new proposal.
Just using std::is_nothrow_copy_constructible<_Ite> seems simpler,
that will be true for __normal_iterator<I, C> if
is_nothrow_copy_constructible<I> is true.
Ok
The definition in include/debug/safe_iterator.tcc should use
std::declval<_Ite>() not declval<_Ite>(). Is there any reason why the
definition uses a late-specified-return-type (i.e. auto and ->) when
the declaration doesn't?
I initially plan to use '->
std::decltype(std::__niter_base(__it.base()))' but this did not compile,
ambiguity issue. So I resort to using std::declval and I could have then
done it the same way as declaration, done now.
Attached is what I'm testing, ok to commit once fully tested ?
François
diff --git a/libstdc++-v3/include/bits/stl_algobase.h
b/libstdc++-v3/include/bits/stl_algobase.h
index e7207f67266..0f73da13172 100644
--- a/libstdc++-v3/include/bits/stl_algobase.h
+++ b/libstdc++-v3/include/bits/stl_algobase.h
@@ -317,12 +317,26 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
{ return __it; }
+#if __cplusplus < 201103L
template<typename _Ite, typename _Seq>
- _GLIBCXX20_CONSTEXPR
_Ite
__niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
std::random_access_iterator_tag>&);
+ template<typename _Ite, typename _Cont, typename _Seq>
+ _Ite
+ __niter_base(const ::__gnu_debug::_Safe_iterator<
+ ::__gnu_cxx::__normal_iterator<_Ite, _Cont>, _Seq,
+ std::random_access_iterator_tag>&);
+#else
+ template<typename _Ite, typename _Seq>
+ _GLIBCXX20_CONSTEXPR
+ decltype(std::__niter_base(std::declval<_Ite>()))
+ __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
+ std::random_access_iterator_tag>&)
+ noexcept(std::is_nothrow_copy_constructible<_Ite>::value);
+#endif
+
// Reverse the __niter_base transformation to get a
// __normal_iterator back again (this assumes that __normal_iterator
// is only used to wrap random access iterators, like pointers).
diff --git a/libstdc++-v3/include/debug/safe_iterator.tcc
b/libstdc++-v3/include/debug/safe_iterator.tcc
index 6eb70cbda04..a8b24233e85 100644
--- a/libstdc++-v3/include/debug/safe_iterator.tcc
+++ b/libstdc++-v3/include/debug/safe_iterator.tcc
@@ -235,13 +235,29 @@ namespace std _GLIBCXX_VISIBILITY(default)
{
_GLIBCXX_BEGIN_NAMESPACE_VERSION
+#if __cplusplus < 201103L
template<typename _Ite, typename _Seq>
- _GLIBCXX20_CONSTEXPR
_Ite
__niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
std::random_access_iterator_tag>& __it)
{ return __it.base(); }
+ template<typename _Ite, typename _Cont, typename _DbgSeq>
+ _Ite
+ __niter_base(const ::__gnu_debug::_Safe_iterator<
+ ::__gnu_cxx::__normal_iterator<_Ite, _Cont>, _DbgSeq,
+ std::random_access_iterator_tag>& __it)
+ { return __it.base().base(); }
+#else
+ template<typename _Ite, typename _Seq>
+ _GLIBCXX20_CONSTEXPR
+ decltype(std::__niter_base(std::declval<_Ite>()))
+ __niter_base(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq,
+ std::random_access_iterator_tag>& __it)
+ noexcept(std::is_nothrow_copy_constructible<_Ite>::value)
+ { return std::__niter_base(__it.base()); }
+#endif
+
template<bool _IsMove,
typename _Ite, typename _Seq, typename _Cat, typename _OI>
_GLIBCXX20_CONSTEXPR