QFlags: add operator== and !=

It's a value type, and we don't need detours through implicit
conversions (which are a real disgrace, as they make nonsense like
`flags != 3.14` well-formed).

[ChangeLog][QtCore][QFlags] Added overloads of operator== and
operator!= between QFlags objects, and between a QFlags object
and an object of the flag's enumeration.

Change-Id: I36701dea8fcd4cc64941e78af58fa7d38a15a8c7
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
bb10
Giuseppe D'Angelo 2021-05-02 02:13:29 +02:00
parent 3a62f9e0c9
commit aafea67cf6
2 changed files with 37 additions and 0 deletions

View File

@ -151,6 +151,19 @@ public:
return on ? (*this |= flag) : (*this &= ~QFlags(flag));
}
friend constexpr inline bool operator==(QFlags lhs, QFlags rhs) noexcept
{ return lhs.i == rhs.i; }
friend constexpr inline bool operator!=(QFlags lhs, QFlags rhs) noexcept
{ return lhs.i != rhs.i; }
friend constexpr inline bool operator==(QFlags lhs, Enum rhs) noexcept
{ return lhs == QFlags(rhs); }
friend constexpr inline bool operator!=(QFlags lhs, Enum rhs) noexcept
{ return lhs != QFlags(rhs); }
friend constexpr inline bool operator==(Enum lhs, QFlags rhs) noexcept
{ return QFlags(lhs) == rhs; }
friend constexpr inline bool operator!=(Enum lhs, QFlags rhs) noexcept
{ return QFlags(lhs) != rhs; }
private:
constexpr static inline Int initializer_list_helper(typename std::initializer_list<Enum>::const_iterator it,
typename std::initializer_list<Enum>::const_iterator end)

View File

@ -538,6 +538,30 @@ static_assert(sizeof(qint64) == 8, "Internal error, qint64 is misdefined");
to seed the calcualtion.
*/
/*!
\fn template <typename Enum> bool operator==(QFlags<Enum> lhs, QFlags<Enum> rhs)
\fn template <typename Enum> bool operator==(QFlags<Enum> lhs, Enum rhs)
\fn template <typename Enum> bool operator==(Enum lhs, QFlags<Enum> rhs)
\since 6.2
\relates QFlags
Compares \a lhs and \a rhs for equality; the two arguments are
considered equal if they represent exactly the same value
(bitmask).
*/
/*!
\fn template <typename Enum> bool operator!=(QFlags<Enum> lhs, QFlags<Enum> rhs)
\fn template <typename Enum> bool operator!=(QFlags<Enum> lhs, Enum rhs)
\fn template <typename Enum> bool operator!=(Enum lhs, QFlags<Enum> rhs)
\since 6.2
\relates QFlags
Compares \a lhs and \a rhs for inequality; the two arguments are
considered different if they don't represent exactly the same value
(bitmask).
*/
/*!
\macro Q_DISABLE_COPY(Class)
\relates QObject