[Interest] QFlags: Typesafe way to check "any flag set"
Giuseppe D'Angelo
giuseppe.dangelo at kdab.com
Wed Feb 18 13:34:07 CET 2026
On 18/02/2026 13:00, Schimkowitsch Robert wrote:
> When I look at qflags.h in 6.10.2, I notice that there is
>
> constexpr inline bool operator!() const noexcept { return !i; }
>
> …but operator bool() is listed in the non-TYPESAFE section.
>
> Does this mean that if I want to check if any flag is set, I need to do
> this?
>
> // Check if any flag is set
>
> if ( !! flags)
>
> { /* do something */ }
>
Are you looking at
> #ifdef QT_TYPESAFE_FLAGS
> constexpr inline explicit operator Int() const noexcept { return i; }
> constexpr inline explicit operator bool() const noexcept { return i; }
> #else
> constexpr inline Q_IMPLICIT operator Int() const noexcept { return i; }
> constexpr inline bool operator!() const noexcept { return !i; }
> #endif
?
In the else branch (non-typesafe) there's an implicit operator int(), so
you can just write if (flags) or if (flags != 0).
https://gcc.godbolt.org/z/zebhah5cr
Note that QT_TYPESAFE_FLAGS is a Qt-internal feature at the moment, and
not enabled for user code. (Qt itself isn't clean, only QtCore builds
with it.)
HTH,
--
Giuseppe D'Angelo | giuseppe.dangelo at kdab.com | Senior Software Engineer
KDAB (France) S.A.S., a KDAB Group company
Tel. France +33 (0)4 90 84 08 53, http://www.kdab.com
KDAB - Trusted Software Excellence
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4850 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20260218/e68b3b19/attachment.bin>
More information about the Interest
mailing list