I usually use enum
with the 'bit-or' or |
together to allow an object has some options. How to make enum class to work with the 'bit-or' feature?
How to make enum class to work with the 'bit-or' feature?
3.2k views Asked by user1899020 At
1
You need to overload the operators for your enum class and implement them by casting to the underlying type:
… of course this could be generalised (using SFINAE and
std::underlying_type
). That C++ doesn’t provide this out of the box is an oversight, in my opinion.Here’s how a general implementation might look like:
This implementation ensures that the overload is only found for enums that are actually acting as flags. To mark an enum as a flag, you need to specialise
is_flag
: