microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
reflection.hpp File Reference

Native P2996 (-freflection) formatters for enums and structs. More...

#include "../microfmt.hpp"
#include "reflect_annotate.hpp"
Include dependency graph for reflection.hpp:

Go to the source code of this file.

Detailed Description

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:

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.