Skip to content

Add fmt/reflect.h and format enums annotated with fmt::as_identifiers - #4885

Open
avikivity wants to merge 1 commit into
fmtlib:mainfrom
avikivity:enum-autoformat
Open

Add fmt/reflect.h and format enums annotated with fmt::as_identifiers#4885
avikivity wants to merge 1 commit into
fmtlib:mainfrom
avikivity:enum-autoformat

Conversation

@avikivity

Copy link
Copy Markdown
Contributor

Format an enum as the identifier of the matching enumerator if the enum is annotated with fmt::as_identifiers:

enum class [[=fmt::as_identifiers]] color { red, green, blue };
fmt::format("{}", color::green); // "green"

Identifiers are retrieved via C++26 reflection (P2996) and the annotation via P3394. FMT_USE_REFLECTION is autodetected and can be overridden by the user; without reflection the header is empty.

Values that don't match any enumerator are formatted as values of the underlying type.

@avikivity
avikivity requested a review from vitaut as a code owner August 13, 2026 12:07
Format an enum as the identifier of the matching enumerator if the enum is
annotated with fmt::as_identifiers:

  enum class [[=fmt::as_identifiers]] color { red, green, blue };
  fmt::format("{}", color::green);  // "green"

Identifiers are retrieved via C++26 reflection (P2996) and the annotation
via P3394. FMT_USE_REFLECTION is autodetected and can be overridden by the
user; without reflection the header is empty.

Values that don't match any enumerator are formatted as values of the
underlying type.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@avikivity

Copy link
Copy Markdown
Contributor Author

v2: apply cmake-format

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

Successfully merging this pull request may close these issues.

1 participant