From 7ca633d9a82f90e5bba5e12ba923bfb0a257af63 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 10 May 2023 18:29:02 +0200 Subject: [PATCH] QArrayDataPointer: add a C++20 ranges-style optional projection to assign() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will be useful for implementing QString::assign(), which otherwise has the problem that it's d_ptr is based on char16_t, but it's assign() is supposed to be able to deal with iterators whose value_type returns QChar. Task-number: QTBUG-106198 Change-Id: I87882bf749b4e21b7b32391167962d3e6bae9983 Reviewed-by: Ivan Solovev Reviewed-by: MÃ¥rten Nordheim --- src/corelib/tools/qarraydatapointer.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/corelib/tools/qarraydatapointer.h b/src/corelib/tools/qarraydatapointer.h index b458dc36267..3839baefcf5 100644 --- a/src/corelib/tools/qarraydatapointer.h +++ b/src/corelib/tools/qarraydatapointer.h @@ -7,6 +7,8 @@ #include #include +#include + QT_BEGIN_NAMESPACE template @@ -305,8 +307,8 @@ public: this->ptr = res; } - template - void assign(InputIterator first, InputIterator last) + template + void assign(InputIterator first, InputIterator last, Projection proj = {}) { // This function only provides the basic exception guarantee. constexpr bool IsFwdIt = std::is_convertible_v< @@ -340,12 +342,12 @@ public: break; } else { do { - (*this)->emplace(size, *first); + (*this)->emplace(size, proj(*first)); } while (++first != last); return; // size() is already correct (and dst invalidated)! } } - *dst = *first; // overwrite existing element + *dst = proj(*first); // overwrite existing element ++dst; ++first; }