From 5eae0ce8008ec4d8641ffdc5eeb221a3da0abfc9 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 6 Jul 2015 12:27:01 +0200 Subject: QScoped(Array)Pointer: canonicalize swapping This includes: - have nothrow member-swap - have ADL non-member swap - not specialize qSwap or std::swap Also prevent QScopedPointer <-> QScopedArrayPointer swaps by overloading swap (both member and non-member) on QScopedArrayPointer. It's not 100% safe, but it's what we're doing elsewhere (QMulti(Map,Hash), say). That's technically a SiC change if users expected (qualified) std::swap to invoke QScopedPointer::swap(), but those users were doing it wrong to begin with, and they now get a compile-error instead of silent pessimization, because generic std::swap() doesn't work on QScopedPointer, due to lack of copy (and thus move) semantics. Change-Id: I3ab5c1668722a2c8ccafc16f57310ce8d4bffbd6 Reviewed-by: Olivier Goffart (Woboq GmbH) --- src/corelib/tools/qscopedpointer.h | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) (limited to 'src/corelib/tools') diff --git a/src/corelib/tools/qscopedpointer.h b/src/corelib/tools/qscopedpointer.h index d98b914757..3e6af97a33 100644 --- a/src/corelib/tools/qscopedpointer.h +++ b/src/corelib/tools/qscopedpointer.h @@ -162,7 +162,7 @@ public: return oldD; } - inline void swap(QScopedPointer &other) + void swap(QScopedPointer &other) Q_DECL_NOTHROW { qSwap(d, other.d); } @@ -189,18 +189,9 @@ inline bool operator!=(const QScopedPointer &lhs, const QScopedPoint } template -Q_INLINE_TEMPLATE void qSwap(QScopedPointer &p1, QScopedPointer &p2) +inline void swap(QScopedPointer &p1, QScopedPointer &p2) Q_DECL_NOTHROW { p1.swap(p2); } -QT_END_NAMESPACE -namespace std { - template - Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QScopedPointer) &p1, QT_PREPEND_NAMESPACE(QScopedPointer) &p2) - { p1.swap(p2); } -} -QT_BEGIN_NAMESPACE - - namespace QtPrivate { template struct QScopedArrayEnsureSameType; @@ -230,6 +221,9 @@ public: return this->d[i]; } + void swap(QScopedArrayPointer &other) Q_DECL_NOTHROW // prevent QScopedPointer <->QScopedArrayPointer swaps + { QScopedPointer::swap(other); } + private: explicit inline QScopedArrayPointer(void *) { // Enforce the same type. @@ -245,6 +239,10 @@ private: Q_DISABLE_COPY(QScopedArrayPointer) }; +template +inline void swap(QScopedArrayPointer &lhs, QScopedArrayPointer &rhs) Q_DECL_NOTHROW +{ lhs.swap(rhs); } + QT_END_NAMESPACE #endif // QSCOPEDPOINTER_H -- cgit v1.2.3