From 883f2dd81f8d85d52130aece540cdf9efa3d0069 Mon Sep 17 00:00:00 2001 From: Sona Kurazyan Date: Tue, 4 Aug 2020 16:13:13 +0200 Subject: Avoid possible ambiguities with QByteArrayView's comparison operators MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit QByteArrayView's comparison operators are declared in the global namespace, which can collide with the ones declared for other types that are implicitly convertible to QByteArrayView (see the example attached to the bugreport). Changing them to be hidden friends will make them visible only when at least one of the operands is a QByteArrayView, so the ambiguity will be avoided. Fixes: QTBUG-85880 Change-Id: Ic3582269d9bda9a2105336ef0f044ea619af37ba Reviewed-by: Marco Bubke Reviewed-by: MÃ¥rten Nordheim --- src/corelib/text/qbytearrayview.h | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/corelib/text') diff --git a/src/corelib/text/qbytearrayview.h b/src/corelib/text/qbytearrayview.h index 36764abfb4..4366c37ed2 100644 --- a/src/corelib/text/qbytearrayview.h +++ b/src/corelib/text/qbytearrayview.h @@ -289,20 +289,25 @@ public: [[nodiscard]] constexpr char first() const { return front(); } [[nodiscard]] constexpr char last() const { return back(); } + friend inline bool operator==(QByteArrayView lhs, QByteArrayView rhs) noexcept + { return lhs.size() == rhs.size() && QtPrivate::compareMemory(lhs, rhs) == 0; } + friend inline bool operator!=(QByteArrayView lhs, QByteArrayView rhs) noexcept + { return !(lhs == rhs); } + friend inline bool operator< (QByteArrayView lhs, QByteArrayView rhs) noexcept + { return QtPrivate::compareMemory(lhs, rhs) < 0; } + friend inline bool operator<=(QByteArrayView lhs, QByteArrayView rhs) noexcept + { return QtPrivate::compareMemory(lhs, rhs) <= 0; } + friend inline bool operator> (QByteArrayView lhs, QByteArrayView rhs) noexcept + { return !(lhs <= rhs); } + friend inline bool operator>=(QByteArrayView lhs, QByteArrayView rhs) noexcept + { return !(lhs < rhs); } + private: qsizetype m_size; const storage_type *m_data; }; Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE); -// QByteArrayView <> QByteArrayView -inline bool operator==(QByteArrayView lhs, QByteArrayView rhs) noexcept { return lhs.size() == rhs.size() && QtPrivate::compareMemory(lhs, rhs) == 0; } -inline bool operator!=(QByteArrayView lhs, QByteArrayView rhs) noexcept { return !(lhs == rhs); } -inline bool operator< (QByteArrayView lhs, QByteArrayView rhs) noexcept { return QtPrivate::compareMemory(lhs, rhs) < 0; } -inline bool operator<=(QByteArrayView lhs, QByteArrayView rhs) noexcept { return QtPrivate::compareMemory(lhs, rhs) <= 0; } -inline bool operator> (QByteArrayView lhs, QByteArrayView rhs) noexcept { return !(lhs <= rhs); } -inline bool operator>=(QByteArrayView lhs, QByteArrayView rhs) noexcept { return !(lhs < rhs); } - template, bool> = true> [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept -- cgit v1.2.3