diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2015-07-06 12:27:01 +0200 |
---|---|---|
committer | Marc Mutz <marc.mutz@kdab.com> | 2016-05-04 05:21:30 +0000 |
commit | 5eae0ce8008ec4d8641ffdc5eeb221a3da0abfc9 (patch) | |
tree | bbd99550b837989bfc4cd462548e75e6f0b7461a | |
parent | f3af7fce4a9174fb80de22cf650435516c2f3796 (diff) |
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) <ogoffart@woboq.com>
-rw-r--r-- | src/corelib/tools/qscopedpointer.h | 20 |
1 files changed, 9 insertions, 11 deletions
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<T, Cleanup> &other) + void swap(QScopedPointer<T, Cleanup> &other) Q_DECL_NOTHROW { qSwap(d, other.d); } @@ -189,18 +189,9 @@ inline bool operator!=(const QScopedPointer<T, Cleanup> &lhs, const QScopedPoint } template <class T, class Cleanup> -Q_INLINE_TEMPLATE void qSwap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) +inline void swap(QScopedPointer<T, Cleanup> &p1, QScopedPointer<T, Cleanup> &p2) Q_DECL_NOTHROW { p1.swap(p2); } -QT_END_NAMESPACE -namespace std { - template <class T, class Cleanup> - Q_INLINE_TEMPLATE void swap(QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p1, QT_PREPEND_NAMESPACE(QScopedPointer)<T, Cleanup> &p2) - { p1.swap(p2); } -} -QT_BEGIN_NAMESPACE - - namespace QtPrivate { template <typename X, typename Y> struct QScopedArrayEnsureSameType; @@ -230,6 +221,9 @@ public: return this->d[i]; } + void swap(QScopedArrayPointer &other) Q_DECL_NOTHROW // prevent QScopedPointer <->QScopedArrayPointer swaps + { QScopedPointer<T, Cleanup>::swap(other); } + private: explicit inline QScopedArrayPointer(void *) { // Enforce the same type. @@ -245,6 +239,10 @@ private: Q_DISABLE_COPY(QScopedArrayPointer) }; +template <typename T, typename Cleanup> +inline void swap(QScopedArrayPointer<T, Cleanup> &lhs, QScopedArrayPointer<T, Cleanup> &rhs) Q_DECL_NOTHROW +{ lhs.swap(rhs); } + QT_END_NAMESPACE #endif // QSCOPEDPOINTER_H |