Boost logo

Boost :

Subject: Re: [boost] [Library idea] Type-safe Flagsets
From: Julien Vernay (julienvernay99_at_[hidden])
Date: 2018-06-25 06:09:05


The aim is that each flag is independent, so you can have two flags active
at a same time, while an enum should have only one value at a time.
Scoped enums do not solve those issues because they can not be combined, if
you want to combine them you first need to used "static_cast" for
converting it to an int, then using & | ^ to combine them.

For predefining standard combination of flags, as UP_LEFT, you can do :

    constexpr auto UP_LEFT =
Direction::Type::Value<Direction::UP,Direction::LEFT>();

Thanks for your answer !

2018-06-25 2:47 GMT+02:00 Gavin Lambert via Boost <boost_at_[hidden]>:

> 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.
>
>
> _______________________________________________
> Unsubscribe & other changes: http://lists.boost.org/mailman
> /listinfo.cgi/boost
>


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