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] Requirement about EnumToName #168

Closed
T-rvw opened this issue May 11, 2023 · 0 comments
Closed

[Preprocessor] Requirement about EnumToName #168

T-rvw opened this issue May 11, 2023 · 0 comments

Comments

@T-rvw
Copy link
Contributor

T-rvw commented May 11, 2023

Define an enum can generate const char* GetXXXName(EnumType e) method automatically.

The first version I implemented is:

// Generate names for enum automatically.
#define DEFINE_ENUM_WITH_NAMES(EnumName, ...) \
	enum class EnumName { __VA_ARGS__, Count }; \
	constexpr std::array<const char*, static_cast<int>(EnumName::Count)> EnumName##Names = { #__VA_ARGS__ }; \
	constexpr const char* Get##EnumName##Name(EnumName enumValue) { return EnumName##Names[static_cast<int>(enumValue)]; }

But #__VA_ARGS__ will convert all enums to a single string. For example, Enum class Foo { A, B, C }; will get "A, B, C". So I need to split by comma in compile time or have a better preprocessor than macro.

After some searchs, I can use BOOST_PP_SEQ_FOR_EACH_I in boost. Or use static reflection lib about enum process such as https://github.com/Neargye/magic_enum.

@T-rvw T-rvw closed this as completed Aug 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant