[Interest] QFlags: Typesafe way to check "any flag set"
Schimkowitsch Robert
Robert.Schimkowitsch at andritz.com
Mon Feb 23 11:52:18 CET 2026
Hi,
thanks for your elaboration!
I'll make myself a reminder to file to bugs (if the new Jira allows me to do that...), one for the QT_TYPESAFE_ENUM and one for the missing convertion to int.
My usecase is like this example https://godbolt.org/z/Y7Yaefvor
I want to know whether a specific flag is set, and return the result as 'bool' from a function, like so:
bool isAskingWhen(Question q)
{
return {q & QuestionFlag::When};
}
As you can see in the CE link, this produces a "narrowing" warning, which I want to avoid.
I guess the proper solution would be
return ((q & QuestionFlag::When) != 0);
...or is there some better option?
Have a great day!
Robert
> -----Original Message-----
> From: Interest <interest-bounces at qt-project.org> On Behalf Of Giuseppe
> D'Angelo via Interest
> Sent: Sunday, 22 February 2026 22:18
> To: interest at qt-project.org
> Subject: Re: [Interest] QFlags: Typesafe way to check "any flag set"
>
> CAUTION: External Sender
> Stop. Review. Verify! Were you expecting this mail?
> If you were not expecting this email, confirm with the sender through a
> trusted channel (e.g., phone or Teams).
> If suspicious, please report the mail, do not click on any links or open
> attachments, and do not enter any ID or password.
>
>
>
> Hi,
>
> Il 19/02/26 08:31, Schimkowitsch Robert ha scritto:
> > Hi Guiseppe,
> >
> > thanks for the reply!
> >
> > QT_TYPESAFE_FLAGS features prominently in the Qt documentation, see
> > e.g
> > https://doc/.
> > qt.io%2Fqt-6%2Fqflags.html%23operator-and-
> eq&data=05%7C02%7Crobert.sch
> >
> imkowitsch%40andritz.com%7Ca09bda655ebc4cffb8dc08de72581984%7C678
> 5298f
> >
> e857464b9b4b807178402632%7C1%7C0%7C639073919876105010%7CUnkno
> wn%7CTWFp
> >
> bGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4z
> MiIsIk
> >
> FOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=NBL6HbErIfvoP
> VY7Vxq%
> > 2BOLxnDEczqQBHD9YHKXm9rVI%3D&reserved=0
>
> I think that's a mistake, as QT_TYPESAFE_FLAGS itself is never documented...
>
> > What I found confusing is that operator!() is documented, but operator
> bool() is not. Also, the implicit conversion to int is not documented (at least I
> could not find it in the above page).
>
> That's also a mistake. Would you mind filing a QTBUG for documentation?
>
> > There is "toInt()", but then I'd have to explicitly write
> >
> > if (flags.toInt())
> > // something
> >
> > I noticed that I can apparently static_cast flags to bool, but if I want to return
> a bool from a function, I have to use the explicit cast (which would be fine, if
> that is the way it's supposed to work).
>
> Going back to:
>
> > #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
>
> if QT_TYPESAFE_FLAGS is not defined (the default), then a QFlags object will
> implicitly convert to bool through the operator Int(), including on function
> return:
>
> https://gcc.god/
> bolt.org%2Fz%2FoPjhM661G&data=05%7C02%7Crobert.schimkowitsch%40an
> dritz.com%7Ca09bda655ebc4cffb8dc08de72581984%7C6785298fe857464b9b
> 4b807178402632%7C1%7C0%7C639073919876130763%7CUnknown%7CTWF
> pbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4
> zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=as1f8Xa
> 5IHoi1Jveknr0IUn0IvismBvsOSjbvBT5VR4%3D&reserved=0
>
>
> If instead it is defined (not recommended), then you can still test them using
> `if (flags)`, but you need an explicit cast when using `return` or similar forms
> of copy initializations, because the operator towards bool becomes `explicit`.
> This is a somewhat annoying quirk of C++ that may cause source
> incompatibilities (cf.
> https://codere/
> view.qt-
> project.org%2Fc%2Fqt%2Fqtbase%2F%2B%2F311100&data=05%7C02%7Crob
> ert.schimkowitsch%40andritz.com%7Ca09bda655ebc4cffb8dc08de72581984
> %7C6785298fe857464b9b4b807178402632%7C1%7C0%7C6390739198761487
> 05%7CUnknown%7CTWFpbGZsb3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjA
> uMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C
> %7C%7C&sdata=sy3z4wL12FyIzfNsthVEpc5x%2Bx7KtqlNdztN7QErfGc%3D&re
> served=0 )
>
> For example,
>
> // under QT_TYPESAFE_FLAGS
> QFlags<E> flags;
>
> bool f() {
> bool ok = flags; // ERROR
> bool ok { flags }; // OK
> if (flags) {} // OK
> return flags; // ERROR
> return bool(flags); // OK ; or (bool)flags, static_cast<bool>(flags), etc.
> }
>
>
>
> 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.kd/
> ab.com%2F&data=05%7C02%7Crobert.schimkowitsch%40andritz.com%7Ca0
> 9bda655ebc4cffb8dc08de72581984%7C6785298fe857464b9b4b80717840263
> 2%7C1%7C0%7C639073919876165597%7CUnknown%7CTWFpbGZsb3d8eyJF
> bXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiT
> WFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=D9e55vakiHcr6TZV0CR
> oKV10mD7HIalN%2BmBq11vXJZU%3D&reserved=0
> KDAB - Trusted Software Excellence
________________________________
This message and any attachments are solely for the use of the intended recipients. They may contain privileged and/or confidential information or other information protected from disclosure. If you are not an intended recipient, you are hereby notified that you received this email in error and that any review, dissemination, distribution or copying of this email and any attachment is strictly prohibited. If you have received this email in error, please contact the sender and delete the message and any attachment from your system.
ANDRITZ HYDRO GmbH
Rechtsform/ Legal form: Gesellschaft mit beschränkter Haftung / Corporation
Firmensitz/ Registered seat: Wien
Firmenbuchgericht/ Court of registry: Handelsgericht Wien
Firmenbuchnummer/ Company registration: FN 61833 g
DVR: 0605077
UID-Nr.: ATU14756806
Thank You
________________________________
More information about the Interest
mailing list