0

I have the following enum:

enum enumLoanPaymentPolicy
{
    Unspecified = 0,

    InterestInArrears = 1 << 1,
    InterestInAdvance = 1 << 2,

    RoundUpRepayments = 1 << 3,
    RoundInterest = 1 << 4,
    RoundUpFlows = 1 << 5,
    RoundingMask = RoundUpRepayments | RoundInterest | RoundUpFlows,
};

Then elsewhere, given a value (foo) of this enumeration, I want to extract the bits set that are pertinent to Round.

I use foo & RoundingMask for that, but what type should I use?

Ideally I'd use somethingorother(enumLoanPaymentPolicy) bar = foo & RoundingMask where somethingorother is a bit like decltype. Is this even possible?

P45 Imminent
  • 8,319
  • 4
  • 35
  • 78

1 Answers1

6

You are looking for std::underlying_type

example code from cppreference:

#include <iostream>
#include <type_traits>

enum e1 {};
enum class e2: int {};

int main() {
    bool e1_type = std::is_same<
        unsigned
       ,typename std::underlying_type<e1>::type>::value;     
    bool e2_type = std::is_same<
        int
       ,typename std::underlying_type<e2>::type>::value;
    std::cout
    << "underlying type for 'e1' is " << (e1_type?"unsigned":"non-unsigned") << '\n'
    << "underlying type for 'e2' is " << (e2_type?"int":"non-int") << '\n';
}
NathanOliver
  • 171,901
  • 28
  • 288
  • 402