[Interest] QFlags: Typesafe way to check "any flag set"
Schimkowitsch Robert
Robert.Schimkowitsch at andritz.com
Mon Feb 23 12:39:51 CET 2026
All clear now, thanks!
> -----Original Message-----
> From: Interest <interest-bounces at qt-project.org> On Behalf Of Giuseppe
> D'Angelo via Interest
> Sent: Monday, 23 February 2026 12:23
> 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.
>
>
>
> Il 23/02/26 11:52, Schimkowitsch Robert ha scritto:
> > 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 you have a couple of options here, you could either:
>
> * not put braces at all, or use round braces instead of curly. Round braces just
> group the expression, and are even redundant here.
> Round braces or not, the `q & QuestionFlag::When` expression has type
> QFlags<X>, and it converts to bool via its implicit operator int().
> Curly braces instead trigger copy-list-initialization of the returned bool again
> via the implicit operator int(), but since int->bool is a narrowing conversion,
> and narrowing conversions are invalid in list initializations, then compiler
> warns or rejects the code; or
>
> * use the slightly more expressive/verbose testFlag (or testAnyFlag depending
> on your use case), e.g.
>
> return q.testFlag(QuestionFlag::When);
>
>
> 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%7Ca3
> 4ae40c78144eb1a92d08de72ce28ec%7C6785298fe857464b9b4b80717840263
> 2%7C1%7C0%7C639074426954119274%7CUnknown%7CTWFpbGZsb3d8eyJF
> bXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkFOIjoiT
> WFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=oMWgSci9oXCo%2F3k
> kuXKQwNDNrrLU6JieVfll3iZvS7o%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