如何知道类枚举的底层类型? [英] How to know underlying type of class enum?
本文介绍了如何知道类枚举的底层类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个变量声明为:
enum class FooEnum: uint64_t {}
,我想转换为其基本类型,但我不想硬编码基本类型。例如,像这样:
and I would like to cast to its base-type, but I don't want to hardcode the base-type. For instance, something like this:
FooEnum myEnum;
uint64_t * intPointer = (underlying_typeof(myEnum))&myEnum;
这是否可能?
推荐答案
您可以使用:
-
std :: underlying_type
类模板来了解枚举的基本类型。 >
std::underlying_type
class template to know the underlying type of enum.
定义类型的成员
typedef
>这是枚举T的底层类型。
Defines a member
typedef
type of type that is the underlying type for the enumeration T.
所以你应该能够这样做:
So you should be able to do this:
#include <type_traits> //include this
FooEnum myEnum;
auto pointer = static_cast<std::underlying_type<FooEnum>::type*>(&myEnum);
这篇关于如何知道类枚举的底层类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文