79449796

Date: 2025-02-18 22:52:59
Score: 1
Natty:
Report link

Right now, imo, the best way (for scoped enum) is with a macro:

#define ENUM_VALUE(x) static_caststd::underlying_type_t<decltype(x)>(x)

That's guaranteed to work everywhere and at compile time, unlike the constexpr patterns.

If it was just about typing less, I wouldn't bother at all... but it's also less error prone than typing out the static_cast because it extracts the base type of the enum class.

But, as someone else mentioned, a very viable alternative is to use unscoped enums within their own namespace.

Reasons:
  • Long answer (-0.5):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Daemonjax