【发布时间】:2020-02-07 03:20:54
【问题描述】:
给定一个作用域枚举,是否可以在不显式指定基础类型的情况下转换为基础类型?
例子:
enum class HeapCorruptionDetectMethod {//default is int
write_on_freed,
buffer_underrun,
buffer_overrun
};
auto active_method = HeapCorruptionDetectMethod::write_on_freed;
//...
//agnostic about the underlying type:
auto index = static_cast<*underlying_type*>(active_method);
也就是说,是否可以查询底层类型?
【问题讨论】:
-
我有点好笑,因为这是字面意思第一次在 Google 上点击“underlying_type”。
-
很抱歉挑词,但
auto并在没有明确提及的情况下获取底层类型我不会称之为“不可知论”。类型仍然存在,您不必明确提及它。index属于特定类型,取决于你如何使用它,它的类型会有所不同