summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2021-12-22 16:01:23 +0100
committerGiuseppe D'Angelo <giuseppe.dangelo@kdab.com>2022-01-13 21:19:24 +0100
commitb1cf7285f9a469b8f0c62f2048e4c0828def5f1a (patch)
tree768548d84a013ad7c07474136bf6a2a7c765a224 /src/corelib
parent552c4a9655ea22c354454132a9bda7a1c7c836f7 (diff)
Properly detect and declare contiguous iterators
The recommended way to detect a contiguous iterator isn't to check the iterator_category; it's to use the iterator concepts. Similarly, the recommendation set in place by P2259 (for being backwards-compatible) is to declare a iterator_concept member, not to change iterator_category to a C++20 category, (also) because legacy code may be checking for equality against a specific category, rather than for convertibility. This is erroneous, but such code exists, alas. This is enshrined in C++20's stdlib: for instance, iterator_traits<Foo*> has random_access_category_tag as iterator_category, but contiguous_iterator_tag as its iterator_concept. Hence: 1) in QArrayDataOps use the concept, and not the category, to do the check 2) when declaring iterators, keep the category as random access, and introduce the concept alias (if supported). Pick-to: 6.2 6.3 Change-Id: Ib600da7331d687a15082becaa6be06aefc24bb9c Reviewed-by: Andrei Golubev <andrei.golubev@qt.io> Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qarraydataops.h14
-rw-r--r--src/corelib/tools/qlist.h11
2 files changed, 9 insertions, 16 deletions
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
index 68a536004f..0de0c1df75 100644
--- a/src/corelib/tools/qarraydataops.h
+++ b/src/corelib/tools/qarraydataops.h
@@ -911,17 +911,13 @@ public:
Q_ASSERT(distance >= 0 && distance <= this->allocatedCapacity() - this->size);
Q_UNUSED(distance);
-#if __cplusplus >= 202002L
- constexpr bool canUseCopyAppend = std::conjunction_v<
- std::is_convertible<
- typename std::iterator_traits<It>::iterator_category,
- std::contiguous_iterator_tag
- >,
- std::is_same<
+#if __cplusplus >= 202002L && defined(__cpp_concepts) && defined(__cpp_lib_concepts)
+ constexpr bool canUseCopyAppend =
+ std::contiguous_iterator<It> &&
+ std::is_same_v<
std::remove_cv_t<typename std::iterator_traits<It>::value_type>,
T
- >
- >;
+ >;
if constexpr (canUseCopyAppend) {
this->copyAppend(std::to_address(b), std::to_address(e));
} else
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h
index 8a2b542357..aaa2a950dd 100644
--- a/src/corelib/tools/qlist.h
+++ b/src/corelib/tools/qlist.h
@@ -147,12 +147,10 @@ public:
using value_type = T;
// libstdc++ shipped with gcc < 11 does not have a fix for defect LWG 3346
#if __cplusplus >= 202002L && (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 11)
- using iterator_category = std::contiguous_iterator_tag;
+ using iterator_concept = std::contiguous_iterator_tag;
using element_type = value_type;
-#else
- using iterator_category = std::random_access_iterator_tag;
#endif
-
+ using iterator_category = std::random_access_iterator_tag;
using pointer = T *;
using reference = T &;
@@ -220,11 +218,10 @@ public:
using value_type = T;
// libstdc++ shipped with gcc < 11 does not have a fix for defect LWG 3346
#if __cplusplus >= 202002L && (!defined(_GLIBCXX_RELEASE) || _GLIBCXX_RELEASE >= 11)
- using iterator_category = std::contiguous_iterator_tag;
+ using iterator_concept = std::contiguous_iterator_tag;
using element_type = const value_type;
-#else
- using iterator_category = std::random_access_iterator_tag;
#endif
+ using iterator_category = std::random_access_iterator_tag;
using pointer = const T *;
using reference = const T &;