是否可以确定类型是否为作用域枚举类型? [英] Is it possible to determine if a type is a scoped enumeration type?
本文介绍了是否可以确定类型是否为作用域枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否存在类型特征,或者可以编写如下类型特征is_scoped_enum<T>
:
Is there a type trait, or is it possible to write a type trait is_scoped_enum<T>
such that:
- 如果
T
是作用域枚举,则is_scoped_enum<T>::value
是true
和 - 如果
T
是其他任何类型,则is_scoped_enum<T>::value
为false
- if
T
is a scoped enumeration,is_scoped_enum<T>::value
istrue
and - if
T
is any other type,is_scoped_enum<T>::value
is false
推荐答案
我认为测试它是否为枚举 和不能隐式转换为基础类型的应该可以解决问题.
I think testing if it is an enum and not implicitly convertible to the underlying type should do the trick.
template <typename T, bool B = std::is_enum<T>::value>
struct is_scoped_enum : std::false_type {};
template <typename T>
struct is_scoped_enum<T, true>
: std::integral_constant<bool,
!std::is_convertible<T, typename std::underlying_type<T>::type>::value> {};
这篇关于是否可以确定类型是否为作用域枚举类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文