How to convert an enum to string in C++

This page summarizes the projects mentioned and recommended in the original post on news.ycombinator.com

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • metalang99

    Full-blown preprocessor metaprogramming

  • There are also other approaches. Macro variants making use of `__VA_ARGS__` would be probably the best trade-off. If you want a slightly more ergonomic syntax, something like Metalang99 [1] will help (and the author even wrote a post about this exact subject [2]). Codegen is another option which may work better than other options depending on the situation and exact implementation strategy. And there is always the Reflection TS [3], which may or may not be incorporated to C++26...

    [1] https://github.com/Hirrolot/metalang99

    [2] https://hirrolot.github.io/posts/pretty-printable-enumeratio...

    [3] https://en.cppreference.com/w/cpp/experimental/reflect

  • winresult

    Debug-friendly rust types/codes for windows results

  • https://github.com/MaulingMonkey/winresult/

    Another project: thindx. Bindings for d3dcompiler + d3d9 + xinput have a lot of enums and flags:

    • 50 results in 49 files for flags!: https://github.com/search?q=repo%3AMaulingMonkey%2Fthindx%20...

    • 77 results in 69 files for enumish!: https://github.com/search?q=repo%3AMaulingMonkey%2Fthindx+en...

    • I still do things by hand somtimes, for reasons that elude my recollection: https://github.com/MaulingMonkey/thindx/blob/127d75f9de91f73...

    And these are baby numbers compared to an actual professional gamedev codebase. Attention to detail makes me fairly comfortable with this much hand-generated nonsense in my one man show, but bugs still crop up... and there are people I would dread handing maintainence of such a project over to that I've worked with in a professional capacity, who simply do not care to exercise the same level of care as I do when editing such stuff.

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • thindx

    Thin DirectX wrappers for Rust.

  • https://github.com/MaulingMonkey/winresult/

    Another project: thindx. Bindings for d3dcompiler + d3d9 + xinput have a lot of enums and flags:

    • 50 results in 49 files for flags!: https://github.com/search?q=repo%3AMaulingMonkey%2Fthindx%20...

    • 77 results in 69 files for enumish!: https://github.com/search?q=repo%3AMaulingMonkey%2Fthindx+en...

    • I still do things by hand somtimes, for reasons that elude my recollection: https://github.com/MaulingMonkey/thindx/blob/127d75f9de91f73...

    And these are baby numbers compared to an actual professional gamedev codebase. Attention to detail makes me fairly comfortable with this much hand-generated nonsense in my one man show, but bugs still crop up... and there are people I would dread handing maintainence of such a project over to that I've worked with in a professional capacity, who simply do not care to exercise the same level of care as I do when editing such stuff.

  • Better Enums

    C++ compile-time enum to string, iteration, in a single header file

  • I really like better_enums instead of magic_enums. There’s no limit on enum size with it: http://aantron.github.io/better-enums/

    It was heavily used at a former employer of mine, so definitely a solid production-ready solution.

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts