Consider std::to_underlying
You can replace static_cast<udl_type>(a)
with std::to_underlying(a)
, and not need to declare udl_type
at all.
This could simplify the code a bit, compared to extracting the type and doing three static_cast
operations on the same line.
Perhaps Check that the Result is a Valid enum
Value
There is unfortunately no way to do this generically. You could, however, define
template<typename T> constexpr bool is_legal_value(const T& input) noexcept {
return true;
}
And then override it for each type where twiddling bits could actually produce an illegal value, such as by comparing to the first and last values, or a switch
statement. Then, you could add to the bitwise functions a block such as:
if (!is_legal_value(result)) {
throw std::logic_error("Bitwise operation yielded illegal enum value.");
}
This should be optimized out and have zero cost if the code uses the trivial default implementation.