如何得到枚举的基本类型

How do I get the fundamental type of an enum?

本文关键字:类型 枚举 何得      更新时间:2023-10-16

声明如下:

enum DrawBoldMode : unsigned
{
    DBM_NONE =              0,
    DBM_ITEM =              1<<0,   // bold just the nearest line
    DBM_SECTION =           1<<1,   // bold all lines in the same section
    DBM_LINETYPE =          1<<2,   // bold all lines of the same line type
    DBM_POINTAGE =          1<<3,   // bold all lines of the same line type
};

我如何派生DrawBoldMode(即unsigned)的底层类型?

std::underlying_type在GCC 4.7中可用,但在此之前,您可以使用模板获得近似模拟:

#include <tuple>
// This is a hack because GCC 4.6 does not support std::underlying_type yet.
// A specialization for each enum is preferred
namespace detail {
    template <typename T, typename Acc, typename... In>
    struct filter;
    template <typename T, typename Acc>
    struct filter<T, Acc> {
        typedef typename std::tuple_element<0, Acc>::type type;
    };
    template <typename T, typename... Acc, typename Head, typename... Tail>
    struct filter<T, std::tuple<Acc...>, Head, Tail...>
    : std::conditional<sizeof(T) == sizeof(Head) && (T(-1) < T(0)) == (Head(-1) < Head(0))
                      , filter<T, std::tuple<Acc...,Head>, Tail...>
                      , filter<T, std::tuple<Acc...>, Tail...>
                      >::type {};
    template <typename T, typename... In>
    struct find_best_match : filter<T, std::tuple<>, In...> {};
}
namespace std {
    template <typename E>
    struct underlying_type : detail::find_best_match<E,
                                signed short,
                                unsigned short,
                                signed int,
                                unsigned int,
                                signed long,
                                unsigned long,
                                signed long long,
                                unsigned long long,
                                bool,
                                char,
                                signed char,
                                unsigned char,
                                wchar_t,
                                char16_t,
                                char32_t> {};
}

它不会给你确切的类型,但它会给你一个具有相同大小和签名特征的。

它应该作为std::underlying_type<DrawBoldMode>::type可用。然而,我的编译器(GCC 4.6.1)似乎没有实现这一点。

认为不可能用模板实现它,但我可能错了。