Boost logo

Boost :

Subject: Re: [boost] [Library idea] Type-safe Flagsets
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2018-06-25 00:47:24


On 25/06/2018 06:43, Julien Vernay wrote:
> I think there are some issues with this kind of flagset :
> - you need to think about power-of-twos when attributing values to your
> flags.
> - you need to use bitwise operations which can be non-trivial, even for
> removing only one flag.
> - it is not safe:
> for example with another flagset using the flag INIT_VIDEO (in a window
> library like SDL),
> you can do " UP | INIT_VIDEO " even if the two flagsets have nothing in
> common

Note that C++11's scoped enums solve those safety issues.

And if you don't care about the specific bit values (eg. you don't need
to directly serialize the value or interoperate with an external API),
then you can use bitfields, which resolve all of those issues (albeit
with other limitations).

Does your approach allow you to predefine standard combinations of flags
(eg. defining UP_LEFT = UP | LEFT)? That's a common requirement for
these sorts of things.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk