Skip to main content
Bounty Ended with 50 reputation awarded by John
added 119 characters in body
Source Link
Davislor
  • 8.4k
  • 17
  • 37

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.

Consider std::to_underlying

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.

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, 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.

Source Link
Davislor
  • 8.4k
  • 17
  • 37

Consider std::to_underlying

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.