Hello,
I recently needed a reflective enum for work (that is an enum where you can
programmatically convert to and from strings, know how many enumerators
there are, and iterate over all enumerators). I looked online but I didn't
find anything that met my requirements. I was surprised that there wasn't
one in Boost. So I'd like to see if there is interest; I wrote an
implementation that I think has several nice features.
The most polished existing smart enum implementation I could find was
http://aantron.github.io/better-enums/. However, the enum implemented there
is not a true enum; it uses a nested member of a class kind of approach. So
for example, the is_enum type trait will not recognize is as an enum. You
cannot use it as a non-type template parameter.
I strongly think that a good generic smart enum macro should generate the
exact "vanilla" enum (or enum class) declaration. My library's syntax looks
like this:
WISE_ENUM(Foo, BAR, (BAZ, 3))
This generates, among other things, exactly:
enum Foo { BAR, BAZ=3};
I intend this as an explicit guarantee of the library that the programmer
can count on. That way when Foo is used for things unrelating to
reflection, the developer can be confident that there will not be any
surprises. Beyond compatability with C++ facilities for enums, and
minimizing surprise, another advantage of this approach is that it's
guaranteed to be backwards compatible. An existing codebase with an
existing enum/enum class can switch to this smart enum with very high
confidence that existing code will continue to work as before.
The WISE_ENUM macro, beyond generating the vanilla enum, also generates a
switch-case function for to-string conversion (for maximal efficiency), and
finally it generates a constexpr function that returns a
std::array