From 1219dbcd123a8001785a1e58452d9abfdfc43471 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 2 Oct 2023 18:27:51 +0200 Subject: [PATCH] QPointer: ensure construction from nullptr is constinit'able Construction from nullptr wasn't, before, because it was using the QPointer(T*) constructor, which cannot be constexpr. Add a constexpr QPointer(std::nullptr_t) constructor to enable this use-case. This requires to mark the (T*) constructor as Q_WEAK_OVERLOAD, otherwise legacy construction from a literal 0 would be ambiguous. No documentation changes needed, as the set of valid expressions (apart from constinit'ing) has not changed. Mention the nullptr ctor, though, without \since. Add a test to confirm that contruction from derived still works. Change-Id: If9d5281f6eca0c408a69f03fecba64a70a0c9cf0 Reviewed-by: Thiago Macieira --- src/corelib/kernel/qpointer.cpp | 1 + src/corelib/kernel/qpointer.h | 3 +++ tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp | 7 +++++++ 3 files changed, 11 insertions(+) diff --git a/src/corelib/kernel/qpointer.cpp b/src/corelib/kernel/qpointer.cpp index bb6fcc287e2..b82f736170a 100644 --- a/src/corelib/kernel/qpointer.cpp +++ b/src/corelib/kernel/qpointer.cpp @@ -76,6 +76,7 @@ /*! \fn template QPointer::QPointer() + \fn template QPointer::QPointer(std::nullptr_t) Constructs a guarded pointer with value \nullptr. diff --git a/src/corelib/kernel/qpointer.h b/src/corelib/kernel/qpointer.h index 8d63f2bc0ed..39e4ba3e0f2 100644 --- a/src/corelib/kernel/qpointer.h +++ b/src/corelib/kernel/qpointer.h @@ -30,6 +30,9 @@ public: Q_NODISCARD_CTOR QPointer() noexcept = default; Q_NODISCARD_CTOR + constexpr QPointer(std::nullptr_t) noexcept : QPointer{} {} + Q_WEAK_OVERLOAD + Q_NODISCARD_CTOR inline QPointer(T *p) : wp(p, true) { } // compiler-generated copy/move ctor/assignment operators are fine! // compiler-generated dtor is fine! diff --git a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp index bade09be047..e0612948a12 100644 --- a/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp +++ b/tests/auto/corelib/kernel/qpointer/tst_qpointer.cpp @@ -39,15 +39,22 @@ private slots: // check that nullptr QPointer construction is Q_CONSTINIT: [[maybe_unused]] Q_CONSTINIT static QPointer s_file1; [[maybe_unused]] Q_CONSTINIT static QPointer s_file2 = {}; +[[maybe_unused]] Q_CONSTINIT static QPointer s_file3 = nullptr; +[[maybe_unused]] Q_CONSTINIT static QPointer s_file4 = 0; // legacy nullptr void tst_QPointer::constructors() { + struct Derived : QObject {}; + Derived derived; + QPointer p1; QPointer p2(this); QPointer p3(p2); + QPointer p4 = &derived; QCOMPARE(p1, QPointer(0)); QCOMPARE(p2, QPointer(this)); QCOMPARE(p3, QPointer(this)); + QCOMPARE(p4, &derived); } void tst_QPointer::ctad()