将int静态转换为枚举而不是直接对其进行初始化有什么好处吗?

问题描述 投票:1回答:1

以任何方式更安全/“更好”地这样做:

enum ENUM { // can be enum class too
...
};
auto e = static_cast<ENUM>(5);

than

auto e = ENUM(5); // more compact and readable, faster to type

EDIT:用于进程内和/或网络通信,其中枚举仅在消息中序列化然后直接转换。

c++ enums casting static-cast
1个回答
1
投票

Imho,直接初始化就像:

© www.soinside.com 2019 - 2024. All rights reserved.