totally_ordered_wrapper: make conversion from nullptr implicit

It's harmless and enables the pointer-idiomatic assignment of nullptr
without having to resort to reset().

Found in API-Review.

Change-Id: Id3a02f55b3578a29943f729d8d734fc3b1d3af11
Reviewed-by: Rym Bouabid <rym.bouabid@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
(cherry picked from commit 62d3b3c680ed2c6ca417f30fb3ca57482339c2c1)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2024-06-12 11:36:00 +02:00 committed by Qt Cherry-pick Bot
parent 5efc98981b
commit da4c0de7fb
3 changed files with 12 additions and 0 deletions

View File

@ -648,6 +648,9 @@ class totally_ordered_wrapper
P ptr;
public:
totally_ordered_wrapper() noexcept = default;
Q_IMPLICIT constexpr totally_ordered_wrapper(std::nullptr_t)
// requires std::is_pointer_v<P>
: totally_ordered_wrapper(P{nullptr}) {}
explicit constexpr totally_ordered_wrapper(P p) noexcept : ptr(p) {}
constexpr P get() const noexcept { return ptr; }

View File

@ -58,6 +58,8 @@ private Q_SLOTS:
// Add new test cases to tst_qcomparehelpers1.cpp, because minGW already
// complains about a too large tst_qcomparehelpers.cpp.obj object file
void compareWithAttributes();
void totallyOrderedWrapperBasics();
};
#endif // TST_QCOMPAREHELPERS_H

View File

@ -54,3 +54,10 @@ void tst_QCompareHelpers::compareWithAttributes()
#undef COMPARE
}
void tst_QCompareHelpers::totallyOrderedWrapperBasics()
{
Qt::totally_ordered_wrapper<int*> pi; // partially-formed
pi = nullptr;
QCOMPARE_EQ(pi.get(), nullptr);
}