Avoid possible ambiguities with QByteArrayView's comparison operators

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 <marco.bubke@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Sona Kurazyan 2020-08-04 16:13:13 +02:00
parent 8a91947423
commit 883f2dd81f

View File

@ -289,20 +289,25 @@ public:
[[nodiscard]] constexpr char first() const { return front(); } [[nodiscard]] constexpr char first() const { return front(); }
[[nodiscard]] constexpr char last() const { return back(); } [[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: private:
qsizetype m_size; qsizetype m_size;
const storage_type *m_data; const storage_type *m_data;
}; };
Q_DECLARE_TYPEINFO(QByteArrayView, Q_PRIMITIVE_TYPE); 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<typename QByteArrayLike, template<typename QByteArrayLike,
std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true> std::enable_if_t<std::is_same_v<QByteArrayLike, QByteArray>, bool> = true>
[[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept [[nodiscard]] inline QByteArrayView qToByteArrayViewIgnoringNull(const QByteArrayLike &b) noexcept