diff --git a/src/corelib/global/q20algorithm.h b/src/corelib/global/q20algorithm.h index 2918a679e19..bdfb3268130 100644 --- a/src/corelib/global/q20algorithm.h +++ b/src/corelib/global/q20algorithm.h @@ -27,11 +27,53 @@ QT_BEGIN_NAMESPACE namespace q20 { -// like std::is_sorted{,_until} (ie. constexpr) +// like std::is_sorted{,_until}, std::copy (ie. constexpr) #ifdef __cpp_lib_constexpr_algorithms +using std::copy; +using std::copy_if; +using std::copy_n; using std::is_sorted_until; using std::is_sorted; #else +template +constexpr OutputIterator +copy(InputIterator first, InputIterator last, OutputIterator dest) +{ + while (first != last) { + *dest = *first; + ++first; + ++dest; + } + return dest; +} + +template +constexpr OutputIterator +copy_if(InputIterator first, InputIterator last, OutputIterator dest, UnaryPredicate pred) +{ + while (first != last) { + if (pred(*first)) { + *dest = *first; + ++dest; + } + ++first; + } + return dest; +} + +template +constexpr OutputIterator +copy_n(InputIterator first, Size n, OutputIterator dest) +{ + while (n > Size{0}) { + *dest = *first; + ++first; + ++dest; + --n; + } + return dest; +} + template > constexpr ForwardIterator is_sorted_until(ForwardIterator first, ForwardIterator last, BinaryPredicate p = {}) diff --git a/src/corelib/tools/qoffsetstringarray_p.h b/src/corelib/tools/qoffsetstringarray_p.h index 93360a39257..40834871751 100644 --- a/src/corelib/tools/qoffsetstringarray_p.h +++ b/src/corelib/tools/qoffsetstringarray_p.h @@ -20,6 +20,7 @@ #include +#include #include #include #include @@ -100,7 +101,7 @@ constexpr auto makeStaticString(Extractor extract, const T &... entries) const char *strings[] = { extract(entries).operator const char *()... }; size_t lengths[] = { sizeof(extract(T{}))... }; for (size_t i = 0; i < std::size(strings); ++i) { - copyData(strings[i], lengths[i], result.begin() + offset); + q20::copy_n(strings[i], lengths[i], result.begin() + offset); offset += lengths[i]; } return result; @@ -110,7 +111,7 @@ template struct StaticString { char value[N] = {}; constexpr StaticString() = default; - constexpr StaticString(const char (&s)[N]) { copyData(s, N, value); } + constexpr StaticString(const char (&s)[N]) { q20::copy_n(s, N, value); } constexpr operator const char *() const { return value; } };