This question already has answers here:
Can an enum class be converted to the underlying type?
                                
                                    (3个答案)
                                
                        
                                在27天前关闭。
            
                    
可能有必要衰减强类型枚举。
怎么做?

最佳答案

template<typename T, typename U = bool >
struct DecayEnum { using type = T; };

template<typename T>
struct DecayEnum<T, std::enable_if_t<std::is_enum<T>::value, bool> > {
    using type = typename std::underlying_type<T>::type;
};

template <typename T>
typename DecayEnum<T>::type EnumIntegralValue(T& value) {
    return static_cast<typename DecayEnum<T>::type>(value);
}

关于c++ - C++如何衰减强类型枚举? ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58965480/

10-10 13:40