* [committed] libstdc++: Simplify __normal_iterator converting constructor
@ 2021-10-01 19:42 Jonathan Wakely
0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2021-10-01 19:42 UTC (permalink / raw)
To: libstdc++, gcc-patches
[-- Attachment #1: Type: text/plain, Size: 1239 bytes --]
This uses C++11 features to simplify the definition of the
__normal_iterator constructor that allows converting from iterator to
const_iterator. The previous definition relied on _Container::pointer
which is present in std::vector and std::basic_string, but is not
actually part of the container requirements.
Removing the use of _Container::pointer and defining it in terms of
is_convertible allows __normal_iterator to be used with new container
types which do not define a pointer member. Specifically, this will
allow it to be used in std::basic_stacktrace.
In theory this will enable some conversions which were not previously
permitted, for example __normal_iterator<volatile T*, vector<T>> can
now be converted to __normal_iterator<const volatile T*, vector<T>>.
In practice this doesn't matter because the library never uses such
types. In any case, allowing those conversions is consistent with
the corresponding constructors of std::reverse_iterator and
std::move_iterator.
Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:
* include/bits/stl_iterator.h (__normal_iterator): Simplify
converting constructor and do not require _Container::pointer.
Tested powerpc64le-linux. Committed to trunk.
[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 3127 bytes --]
commit fb4d55ef61ca3191ec946d4d41e0e715f4cc4197
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Thu May 6 13:44:36 2021
libstdc++: Simplify __normal_iterator converting constructor
This uses C++11 features to simplify the definition of the
__normal_iterator constructor that allows converting from iterator to
const_iterator. The previous definition relied on _Container::pointer
which is present in std::vector and std::basic_string, but is not
actually part of the container requirements.
Removing the use of _Container::pointer and defining it in terms of
is_convertible allows __normal_iterator to be used with new container
types which do not define a pointer member. Specifically, this will
allow it to be used in std::basic_stacktrace.
In theory this will enable some conversions which were not previously
permitted, for example __normal_iterator<volatile T*, vector<T>> can
now be converted to __normal_iterator<const volatile T*, vector<T>>.
In practice this doesn't matter because the library never uses such
types. In any case, allowing those conversions is consistent with
the corresponding constructors of std::reverse_iterator and
std::move_iterator.
Signed-off-by: Jonathan Wakely <jwakely@redhat.com>
libstdc++-v3/ChangeLog:
* include/bits/stl_iterator.h (__normal_iterator): Simplify
converting constructor and do not require _Container::pointer.
diff --git a/libstdc++-v3/include/bits/stl_iterator.h b/libstdc++-v3/include/bits/stl_iterator.h
index 8517652a173..df774eeb63f 100644
--- a/libstdc++-v3/include/bits/stl_iterator.h
+++ b/libstdc++-v3/include/bits/stl_iterator.h
@@ -1022,6 +1022,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
typedef std::iterator_traits<_Iterator> __traits_type;
+#if __cplusplus >= 201103L
+ template<typename _Iter>
+ using __convertible_from
+ = std::__enable_if_t<std::is_convertible<_Iter, _Iterator>::value>;
+#endif
+
public:
typedef _Iterator iterator_type;
typedef typename __traits_type::iterator_category iterator_category;
@@ -1042,12 +1048,20 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
: _M_current(__i) { }
// Allow iterator to const_iterator conversion
+#if __cplusplus >= 201103L
+ template<typename _Iter, typename = __convertible_from<_Iter>>
+ _GLIBCXX20_CONSTEXPR
+ __normal_iterator(const __normal_iterator<_Iter, _Container>& __i)
+ noexcept
+#else
+ // N.B. _Container::pointer is not actually in container requirements,
+ // but is present in std::vector and std::basic_string.
template<typename _Iter>
- _GLIBCXX20_CONSTEXPR
__normal_iterator(const __normal_iterator<_Iter,
typename __enable_if<
- (std::__are_same<_Iter, typename _Container::pointer>::__value),
- _Container>::__type>& __i) _GLIBCXX_NOEXCEPT
+ (std::__are_same<_Iter, typename _Container::pointer>::__value),
+ _Container>::__type>& __i)
+#endif
: _M_current(__i.base()) { }
// Forward iterator requirements
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-10-01 19:42 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-01 19:42 [committed] libstdc++: Simplify __normal_iterator converting constructor Jonathan Wakely
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).