|
microfmt
Zero-allocation C++ formatting for constrained environments
|
Native P2996 (-freflection) formatters for enums and structs.
More...

Go to the source code of this file.
Native P2996 (-freflection) formatters for enums and structs.
Experimental header providing the same "reflected enum" and "reflected
struct" formatting boost_describe.hpp offers, without depending on Boost or requiring a per-field description macro:
formatter<E> that renders the enumerator's declared name (falling back to the underlying integer value for an unmapped/invalid value), unconditionally – no opt-in needed, since no other builtin formatter targets raw enum types;formatter<T> that renders {name: value, ...} for its public non-static data members, but only once explicitly opted in via MICROFMT_REFLECT_FORMAT(Type) (or a direct enable_reflect_format<Type> specialization) – mirroring Rust's #[derive(Debug)], which is likewise per-type opt-in rather than a blanket default, both to avoid silently formatting a type an author intends to hand-write a formatter for later, and to avoid an ambiguous partial-specialization error against any other enable_if-based formatter<T> (including boost_describe.hpp's) that might also match the same T.This header's own content only exists on a P2996-capable compiler (GCC 16+ trunk as of this writing) built with -std=c++26 -freflection: under any other build, including it compiles to nothing at all, the same way reflect_annotate.hpp behaves, so a project can include this header unconditionally without its own #if RELOCO_HAS_REFLECTION guard around the #include – simply do not rely on the formatters it would have defined when the feature isn't enabled.
MICROFMT_REFLECT_FORMAT and enable_reflect_format are declared in reflect_annotate.hpp, not here, and that header alone – not this one – is what a type's own header should include: reflect_annotate.hpp never requires -freflection either, so the same annotated type definition compiles under every toolchain. Only an application that wants the live formatting this header provides needs to include this header itself (and needs -freflection for that formatting to exist).
A build that cannot use -freflection at all is not left out: see tools/reflect_dump/ for a code generator that runs the same reflection this header uses once, offline, on a -freflection toolchain, and writes out an equivalent, plain-C++ formatter<T>/formatter<E> specialization per type – no reflection syntax at all – for any ordinary compiler to consume instead.
See docs/reflection.md for the full writeup (shared with jplcz_reloco) and docs/formatters/ranges-and-structures.md for usage.