From 6535b6d5f3db3e3afbb9f011207399a16b1dc961 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 28 Oct 2024 19:21:13 +0100 Subject: [PATCH] QList: fix std::to_address(QList::iterator) on older compilers Need to enable element_type unconditionally, to direct the default std::pointer_traits::to_address to fall back to operator->(). [ChangeLog][QtCore][QList] Fixed std::to_address() on QList::iterator on older compilers. Fixes: QTBUG-130643 Change-Id: I521f327933c51ca87c511a7741b2dce65094032b Reviewed-by: Thiago Macieira Reviewed-by: Giuseppe D'Angelo (cherry picked from commit b8c879f273533d87f52dbc8e9d2f9b3b3566cd60) Reviewed-by: Qt Cherry-pick Bot --- src/corelib/tools/qlist.h | 4 ++-- tests/auto/corelib/tools/qlist/tst_qlist.cpp | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 6f98ef4d5bb..60c48d30e58 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -120,8 +120,8 @@ public: using value_type = T; #ifdef QT_COMPILER_HAS_LWG3346 using iterator_concept = std::contiguous_iterator_tag; - using element_type = value_type; #endif + using element_type = value_type; using iterator_category = std::random_access_iterator_tag; using pointer = T *; using reference = T &; @@ -190,8 +190,8 @@ public: using value_type = T; #ifdef QT_COMPILER_HAS_LWG3346 using iterator_concept = std::contiguous_iterator_tag; - using element_type = const value_type; #endif + using element_type = const value_type; using iterator_category = std::random_access_iterator_tag; using pointer = const T *; using reference = const T &; diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index d879467da37..9ebfd7aee05 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -15,6 +15,7 @@ #include #include +#include #ifdef QT_COMPILER_HAS_LWG3346 # if __has_include() @@ -340,6 +341,7 @@ private slots: void swapInt() const { swap(); } void swapMovable() const { swap(); } void swapCustom() const { swap(); } + void toAddress() const; void toList() const; #if QT_VERSION < QT_VERSION_CHECK(6,0,0) void fromStdVector() const; @@ -2935,6 +2937,23 @@ void tst_QList::startsWith() const QVERIFY(myvec.startsWith(1)); } +void tst_QList::toAddress() const +{ + // Annoyingly, QList::iterator is a class; make sure std::to_address works on them + QList l = {1, 2, 3, 4, 5}; + auto check = [&](auto b, auto e) { + QCOMPARE_EQ(q20::to_address(b), l.data()); + QCOMPARE_EQ(q20::to_address(e), l.data() + l.size()); + }; + // begin QTBUG-130643 + check(l.begin(), l.end()); + check(l.cbegin(), l.cend()); + // end QTBUG-130643 + // for reverse_iterator, account for the off-by-one to its ::base(): + check(l.rend() - 1, l.rbegin() - 1); + check(l.crend() - 1, l.crbegin() - 1); +} + template void tst_QList::swap() const {