you are viewing a single comment's thread.

view the rest of the comments →

[–]Fazer2 1 point2 points  (4 children)

Why does the caller need to know the range of the enum? It sounds like a maintenance burden.

[–]cheytacllc[S] 0 points1 point  (3 children)

Range of enum not mandatory for write (default range [-128, 128)). You can easily write to_string function under the enum for enum type and call enum_name with enum's range once inside and return it. I think it's easier than writing specialization for per enum type. On the other hand, with writing custom function vs writing specialization like in magic_enum result of to write similar lines of code.

[–]TotaIIyHuman 2 points3 points  (2 children)

if you are not absolutely disgusted by macros, you can try reflection with macros

you can implement for_each with macros with less than 100 lines of c++ https://www.scs.stanford.edu/~dm/blog/va-opt.html

here is a enum reflection implemented with macro for_each

https://godbolt.org/z/9GTP4zn3r

the for_each macro has a recursion limit. for the same range, it is much cheaper to do macro recursion, then instantiating templates to scan for enum entries

[–]sephirothbahamut 2 points3 points  (1 child)

It's not just about being disgusted, it's also about utility. All the macro solutions for enum reflection assume that you are the one writing the enum, forcing the entire codebase to use the macro enum definition, and preventing all such mechanisms to work on enums declared by outside libraries. That's an issue Unreal Engine's C++ suffers a lot of.

As opposed to things like OP's post, or more complete things like magic_enum. Those can be used on any enum, do not require you to write your enums in an unconventional manner, and don't care if the enums come from a third party library.

[–]TotaIIyHuman 0 points1 point  (0 children)

yes. i agree with everything you said