Skip to content

Conversation

@yaito3014
Copy link
Owner

@yaito3014 yaito3014 commented Jun 30, 2025

@yaito3014
Copy link
Owner Author

yaito3014 commented Jul 1, 2025

Why is Windows build failing?
Compilation succeeded in previous build...

P.S.
MSVC's previous version is 14.43.34808, current version is 14.44.35207.
Maybe regression or something?

@saki7 saki7 added the enhancement New feature or request label Jul 7, 2025
@saki7
Copy link
Collaborator

saki7 commented Jul 7, 2025

I think the basic implementation is OK for a PoC quality; I'll be mentioning some functional issues in review.

@saki7 saki7 added the upstream-workaround Temporary workaround for upstream defects. This must be reviewed and reverted later, if possible label Jul 7, 2025

template <class E>
requires std::is_enum_v<E>
constexpr std::string_view enum_to_string(E value)
Copy link
Collaborator

@saki7 saki7 Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. The basic name for this functionality should be to_identifier, not to_string. We need noexcept and [[nodiscard]]; we can treat this non-throwing version as the default (preferred) function. Since this version has the fallback value, it should be named to_identifier_or.
  2. In addition to the current version, we need another version that throws class bad_enum_access : std::exception for nonexistent values . Note that, even if the predefined enum members can be statically determined by reflection, there are still dynamic values for flag-like enums. This throwing version should be named to_identifier.
  3. Additionally, I think we can provide yet another version that delegates to std::to_chars in case of nonexistent value. This version can be named as e.g. to_identifier_or_underlying.

return std::meta::identifier_of(enumerator);
}
}
return "<unnamed>";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be customizable via function parameter; something similar to std::optional::value_or is needed

} // namespace yk

#endif

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need some utility for adapting user-defined enums.

Such functionality should enable std::formatter and operator<<, and it should require minimal effort on the user's part.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm considering yk::enum_serializer like std::formatter, defaults to use yk::enum_to_identifier. What do you think?

@saki7
Copy link
Collaborator

saki7 commented Jul 7, 2025

I think we can also provide a more rich version that resolves combination of flag-like values (e.g. e::foo_bit | e::bar_bit) then stringifies to "foo_bit | bar_bit". It's going to require more implementation technique so I would rather expect it for future versions; it's not required for this initial PR I think. I plan to open an another issue for this feature request, if there are no objections.

@saki7
Copy link
Collaborator

saki7 commented Jul 7, 2025

Since we have this serialization feature now, I think we should also have the opposite one, i.e. enum parser. We should determine the priority for the parser implementation; I'm not sure if it should be included in the initial PR.

requires std::is_enum_v<E>
constexpr std::string_view enum_to_string(E value)
{
if constexpr (std::meta::is_enumerable_type(^^E))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this condition really needed? Do there exist such case that std::is_enum_v<E> == true && std::meta::is_enumerable_type(^^E) == false ?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_enumerable_type returns false if E is incomplete type, which results to prevent enumerators_of being constant expression.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood. I think we need a test case for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request upstream-workaround Temporary workaround for upstream defects. This must be reviewed and reverted later, if possible

Projects

None yet

Development

Successfully merging this pull request may close these issues.

enum stringize library (C++26)

3 participants