避免硬编码枚举类型

Avoid hardcoding enum type

本文关键字:枚举 类型 编码      更新时间:2023-10-16

c++11代码中,每次使用枚举值时避免提及特定的enum限定符会很好-因为它是一个新代码,并且它被重构了很多。

出于这个目的,是否有可能像这段伪代码的最后一行那样:

enum abc { a,b,c };
// some long code of events which returns the enum's value
auto e = []()->abc{return abc::b;}();
if (e == std::declval(e)::a) { ...

如果在C++11中不可能,在C++14或17中可能吗?

你接近了,你可以使用decltype:

if (e == decltype(e)::a) {
    ...