元编程构造返回枚举的基础类型和迭代器的整数

You*_*008 4 c++ enums templates metaprogramming type-traits

我需要编写一个元编程构造,当给定枚举类型时,它将返回该枚举的基础类型,但是当给定整数时,它将返回该整数。

例如:

enum Enum : short { VALUE1, VALUE2 };

int_type<long>::type  // -> long
int_type<Enum>::type  // -> short
Run Code Online (Sandbox Code Playgroud)

我尝试了这个

template< typename Type >
struct int_type {
    using type = typename std::enable_if< std::is_enum<Type>::value, typename std::underlying_type<Type>::type >::type;
};
template< typename Type >
struct int_type {
    using type = typename std::enable_if< std::is_integral<Type>::value, Type >::type;
};
Run Code Online (Sandbox Code Playgroud)

但它抱怨重新定义该结构。

我也试过了,但是

template< typename Type >
struct int_type {
    using type = typename std::enable_if< std::is_enum<Type>::value, typename std::underlying_type<Type>::type >::type;
    using type = typename std::enable_if< std::is_integral<Type>::value, Type >::type;
};
Run Code Online (Sandbox Code Playgroud)

但随后它抱怨成员的重新定义type

这是我的元编程技能的终点,有人可以帮忙吗?

编辑:我也应该提到,我们的项目仅限于C ++ 11。

Jon*_*ely 5

尝试的问题是,您要定义两次相同的内容,尽管其中一部分使用了enable_if该功能,但是如果enable_ifis不正确,则不会使整个封闭定义无效。也就是说,int_type::type即使其中一个(或两个)无效,您也有两个定义。

您正在寻找的是std::conditional

template< typename Type >
struct int_type {
    using when_enum
      = std::underlying_type<Type>;
    using when_integral
      = std::enable_if<std::is_integral<Type>::value, Type>;
    using type
        = typename std::conditional< std::is_enum<Type>::value,
            when_enum, when_integral
            >::type::type;
};
Run Code Online (Sandbox Code Playgroud)

或在C ++ 14及更高版本中:

template< typename Type >
struct int_type {
    using when_enum
      = std::underlying_type<Type>;
    using when_integral
      = std::enable_if<std::is_integral<Type>::value, Type>;
    using type
        = typename std::conditional_t< std::is_enum<Type>::value,
            when_enum, when_integral
            >::type;
};
Run Code Online (Sandbox Code Playgroud)

但我想我可能会将其写为别名模板:

template< typename Type >
  using int_type = std::conditional_t<
    std::is_enum<Type>::value,
    std::underlying_type<Type>,
    std::enable_if<std::is_integral<Type>::value, Type>>;
Run Code Online (Sandbox Code Playgroud)

也许可以进一步简化它并int_type<T>为实际类型创建别名,所以您无需说typename int_type<T>::type

template< typename Type >
  using int_type = typename std::conditional_t<
    std::is_enum<Type>::value,
    std::underlying_type<Type>,
    std::enable_if<std::is_integral<Type>::value, Type>>::type;
Run Code Online (Sandbox Code Playgroud)

(注意,在C ++ 17中,您可以使用std::is_enum_v<Type>std::is_integral_v而不是is_xxx<Type>::value。)