是否可以确定类型是否为作用域枚举类型?

编程入门 行业动态 更新时间:2024-10-10 05:24:07
本文介绍了是否可以确定类型是否为作用域枚举类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否存在类型特征,或者可以编写如下类型特征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 is true 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> {};

更多推荐

是否可以确定类型是否为作用域枚举类型?

本文发布于:2023-11-12 10:08:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581187.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   作用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!