Skip to content

Should enum_name(E value) (and maybe others?) be prevented from working with empty enums? #320

@ts826848

Description

@ts826848

I just finished helping a colleague track down some strange test failures that appear to stem from enum_name(E value) unintentionally behaving as if E were an empty enum. It took a bit to track down this specific issue because the code that actually used enum_name(E value) wasn't touched in the problematic commit. Some other unrelated shuffling of code appears to have confused the compiler so it started treating the enum in question as a plain forward declaration rather than its full definition, so enum_name(E value) was returning empty strings instead of the correct names.

This got me thinking - should enum_name(E value) work at all if the enum is empty? enum_name<auto V>() has a static_assert to bail if the returned name is empty, so changing enum_name(E value) to bail on empty enums and/or empty names would arguably make the behaviors match and could help catch unintentional changes like this where a previously-defined enum starts being treated as a forward declaration.

I think this question could also be extended to other parts of what magic_enum offers, but enum_name is definitely the place where I've experienced issues the most.

And if enum_name(E value) should work with empty enums, should it return a std::optional<std::string_view> to distinguish between the "no name due to invalid value/empty enum" and "empty name" cases?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestquestionFurther information is requested

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions