[Interest] QFlags: Typesafe way to check "any flag set"
Giuseppe D'Angelo
giuseppe.dangelo at kdab.com
Mon Feb 23 12:23:23 CET 2026
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.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: Firma crittografica S/MIME
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20260223/018445da/attachment.bin>
More information about the Interest
mailing list