Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preprocessor Usage #257

Open
andrewkcorcoran opened this issue Apr 28, 2023 · 1 comment
Open

Preprocessor Usage #257

andrewkcorcoran opened this issue Apr 28, 2023 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@andrewkcorcoran
Copy link

andrewkcorcoran commented Apr 28, 2023

Various third party libraries provide preprocessor macros that process enum values. A classic example is for serializing and deserializing enums.

namespace ns { enum class MyEnum{ A, B, C, D, E, F, G, H}; }
SERIALIZE(ns, MyEnum, A, B, C, D, E, F, G, H) // expands to some serialization code

It would reduce duplication and errors if magic_enum could support extracting enum values in a preprocessor context

// pseudo code
#define MAGIC_ENUM_VALUES(FULLY_QUALIFIED_ENUM_NAME) magic_enum::enum_values<FULLY_QUALIFIED_ENUM_NAME>[0], ...

which would then allow the user to do something like the following

SERIALIZE(ns, MyEnum, MAGIC_ENUM_VALUES(ns##MyEnum))

or even simpler

// Defined once and reused
#define SERIALIZE_ENUM(NAMESPACE, ENUM) SERIALIZE(NAMESPACE, ENUM, MAGIC_ENUM_VALUES(NAMESPACE##ENUM))

// Called for each enum
SERIALIZE_ENUM(ns, MyEnum)
@unterumarmung
Copy link

It is a nice idea, but I do not think it's possible.
magic_enum extract the info about enums using metaprogramming and AFAIK there is no way to propagate this information to the preprocessor.
Also, if there would be a way to parse enums at preprocessor-time, I'm sure someone would have already implemented it.

@Neargye Neargye added the help wanted Extra attention is needed label May 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants