[PATCH] libstdc++: PSTL dispatch for C++20 range random access iterators

Gonzalo Brito Gadeschi gonzalo.gadeschi@gmail.com
Mon Jul 3 12:25:46 GMT 2023


This patch closes 110512, in which random access iterators
originated from C++20 ranges are dispatched to the sequential
algorithms in the parallel STL because, before this patch,
the parallel STL checks whether an iterator inherits from the
random access iterator tag. This patch extends the check
in C++ >= 20 to also check whether the iterator models the
std::random_access_iterator concept. This is allowed
by C++23's P2408, which is safe to backport to C++20 because
any application that would break already exhibits undefined
behavior due to pre-condition violation.

libstdc++-v3/ChangeLog:
* pstl: Dispatch C++20 range random access iterators to parallel algorithms.

Bootstrapping and testing
* Tested with x86_64-pc-linux-gnu.

---
 libstdc++-v3/include/pstl/execution_impl.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/include/pstl/execution_impl.h
b/libstdc++-v3/include/pstl/execution_impl.h
index 64f6cc4357a..c17da29141e 100644
--- a/libstdc++-v3/include/pstl/execution_impl.h
+++ b/libstdc++-v3/include/pstl/execution_impl.h
@@ -22,7 +22,15 @@ namespace __internal

 template <typename _IteratorTag, typename... _IteratorTypes>
 using __are_iterators_of = std::conjunction<
-    std::is_base_of<_IteratorTag, typename
std::iterator_traits<std::decay_t<_IteratorTypes>>::iterator_category>...>;
+#if __cplusplus >= 202002L
+    std::disjunction<
+        std::is_base_of<_IteratorTag, typename
std::iterator_traits<std::decay_t<_IteratorTypes>>::iterator_category>,
+        std::integral_constant<bool,
std::random_access_iterator<_IteratorTypes>>
+    >...
+#else   // __cplusplus
+    std::is_base_of<_IteratorTag, typename
std::iterator_traits<std::decay_t<_IteratorTypes>>::iterator_category>...
+#endif  // __cplusplus
+>;

 template <typename... _IteratorTypes>
 using __are_random_access_iterators =
__are_iterators_of<std::random_access_iterator_tag, _IteratorTypes...>;
-- 
2.17.1


More information about the Libstdc++ mailing list