On 9.11.2017 22:23, JiiPee wrote:
> Thanks, but just thinking another thing: is is a good idea at all using
> enums as (option) flags? I read some say online that should use
> constants instead.
In my experience, enums are much better than static constant integer
data members in a class. The latter sometimes require a separate
definition outside of the class, which is a hassle. And "sometimes"
means depending on the usage of the constant, the compiler, the compiler
version and the compiler optimization flags, making this approach pretty
fragile.
Actually it is very easy to define an enum with bit flag values:
enum flags: std::uint8 {
A = (1<<0),
B = (1<<1),
C = (1<<2),
// ...
};
Also, the bitflags are typically combined, and with the enum approach
the result is still legally representable in the same enum type, making
this more encapsulated (especially with 'enum class'). With separate
integer constants there is no separate type and the compiler does not
even warn you if you try to combine incompatible constants.
A drawback of bitflag enums is that one also needs to define several
one-liners like operator|, operator& for each such enum type for using
them smoothly. I believe the new meta-language features will make this a
bit easier in the future.
hth
Paavo