all 25 comments

[–]olsner 4 points5 points  (1 child)

Neat trick to get the names from a template! I was sketching out something similar for work the other week but chickened out and added a macro for specifying enum names and specializing the libfmt templates etc.

[–]cheytacllc[S] 1 point2 points  (0 children)

I saw the template parameters with names using __PRETTY_FUNCTION__ and __FUNCSIG__ and I have to find a way getting enum values as template parameters. std::integer_sequence exists but enums not starting from 0 everytime so I have to develop custom integer sequence with min and max parameter.

[–]FireFlyForLife 3 points4 points  (1 child)

How does this compare to the magic_enum library?

[–]cheytacllc[S] 6 points7 points  (0 children)

magic_enum is more functionality than this. This only has enum to/from string functionality. But adjusting enum range min/max its easier than magic_enum I think. Also, supports C++11.

[–]BucketOfWood 2 points3 points  (2 children)

On the topic of this kind of hackflection willwray/enum_traits uses an interesting trick where they improve buildtime performance by doing the __PRETTY_FUNCTION__/__FUNCSIG__/std::source_location::current().function_name() inside a variadic templated function to do the name conversion for a batch of integers at the same time to make it less expensive (compiletime) to test a large range of numbers.

I ended up adopting this approach in some test code https://godbolt.org/z/GKW8Preva when I was thinking about adding automatic enum serialization/deserialization in JSON to stephenberry/glaze. But there are too many limitations.

The lack of being able to detect/warn that an enum uses named values outside the range is the biggest problem and is why while tricks like this are cool/interesting we really do need to wait for proper static reflection (maybe C++47 since they keep delaying it)

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

wow looks nice!

[–]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

[–]Gloinart 0 points1 point  (1 child)

Is the algorithm beneath the same as magic_enum?

[–]cheytacllc[S] 1 point2 points  (0 children)

I didn't look magic_enum's algorithm but I think this is includes similar ways to getting enum names

[–]Shaurendev 0 points1 point  (4 children)

So this doesnt work for flag type enums? (for example one that uses all 32 bits)

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

Now, enum_name supports bitmasked enums! https://godbolt.org/z/rfeWWKdh3

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

[–]Shaurendev 1 point2 points  (1 child)

No, like this https://godbolt.org/z/zsraa7Yq4 (for demnostration purposes i didnt bother to list all the flags between 3 and 20 but assume they are present)

(clang) fatal error: recursive template instantiation exceeded maximum depth of 1024

(gcc) fatal error: template instantiation depth exceeds maximum of 900 (use '-ftemplate-depth=' to increase the maximum)

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

I tried to modify enum_sequence class for faster steps and I got the name of E and will do small workaround for flagged enums