你能得到枚举的基础类型吗?

P45*_*ent 0 c++

我有以下内容enum:

enum enumLoanPaymentPolicy
{
    Unspecified = 0,

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

    RoundUpRepayments = 1 << 3,
    RoundInterest = 1 << 4,
    RoundUpFlows = 1 << 5,
    RoundingMask = RoundUpRepayments | RoundInterest | RoundUpFlows,
};
Run Code Online (Sandbox Code Playgroud)

然后在其他地方,给定foo这个枚举的value(),我想提取与之相关的位集Round.

我用foo & RoundingMask它,但我应该使用什么类型

理想情况下我会使用somethingorother(enumLoanPaymentPolicy) bar = foo & RoundingMask其中somethingorother有点像decltype.这甚至可能吗?

Nat*_*ica 6

你在找 std::underlying_type

来自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';
}
Run Code Online (Sandbox Code Playgroud)