How does C++26 static reflection work — the ^^ operator, std::meta::info and splicing — and what can a team build with it?
answer
- compile time only, no runtime metadata
- reflect with ^^, splice with [: :]
- consteval queries in <meta>
- template for over members
- access_context controls private access
basics
~20 sThe ^^ operator turns a type, member or other entity into a std::meta::info value at compile time; consteval functions in <meta> query it, and a splice [: r :] turns a reflection back into code. Nothing exists at run time unless you generate it.
solid answer
~40 sC++26 reflection is **static**: it happens entirely during compilation. **`^^T`** produces a value of type **`std::meta::info`**, an opaque handle to a type, member, enumerator, namespace or other entity. **Consteval** functions in **`<meta>`** query it: `std::meta::nonstatic_data_members_of(^^T, ctx)`, `enumerators_of`, `identifier_of`, `type_of`, `is_public` and many more; `access_context` decides whether private members are visible. A **splice**, `[: r :]`, turns a reflection back into a type, expression or member access, and **expansion statements** (`template for`) iterate over the results. Teams use it for enum-to-string, serialization, struct-of-arrays layouts and replacing macros or code generators. Nothing is kept at run time unless the program generates it, so unused reflection costs only compile time. GCC 16 has experimental support behind `-freflection`; Apple clang 21 has none.
go deeper
Know that C++26 can inspect types at compile time with ^^ and that it adds no runtime metadata, unlike runtime reflection.
Explain std::meta::info, the <meta> queries, splices and template for, and write the outline of an enum-to-string function.
Judge where reflection replaces macros or code generators in a real codebase, and the build-time and review costs it brings.
Decide when the team adopts it: experimental compiler support today, which generators it retires, and the rules for access_context unchecked().
## Static, not runtime Many platforms offer **runtime reflection**: every type carries metadata that code can query while the program runs. C++26 takes the other route. **Static reflection** lets code inspect declarations **during compilation** and use what it finds to generate code. Nothing is added to the binary unless the program asks for it, so there is no per-class metadata and no run-time lookup cost. The trade is that reflection code must run at compile time, in `consteval` functions and constant expressions. ## The three pieces | Piece | Spelling | Meaning | |---|---|---| | reflection operator | `^^T`, `^^ns`, `^^S::member` | produce a reflection of an entity | | reflection value | `std::meta::info` | an opaque, compile-time-only handle; `<meta>` defines it as `decltype(^^::)` | | splice | `[: r :]` | turn a reflection back into the entity it represents | `std::meta::info` values can be compared, stored in `constexpr` variables and passed to `consteval` functions, but they cannot exist at run time. ## Querying with <meta> The `<meta>` header declares the **metafunctions**, all `consteval`. A sample: - `members_of(r, ctx)`, `nonstatic_data_members_of(type, ctx)`, `static_data_members_of`, `bases_of` — members of a class, filtered by an **`access_context`**; - `enumerators_of(type_enum)` — the enumerators, in declaration order; - `identifier_of(r)` and `display_string_of(r)` — names as `std::string_view`; - `type_of(r)`, `is_public(r)`, `offset_of(r)`, `size_of(r)` — properties; - `substitute(templ, args)` — instantiate a template from reflections; - `define_aggregate(type_class, members)` — *generate* a class definition; - `annotations_of(r)` — read user annotations written as `[[=value]]`. `std::meta::access_context::current()` sees what the calling scope may see; `unprivileged()` sees only public members; `unchecked()` sees everything, which is a deliberate, greppable escape hatch. ## Iterating: expansion statements Results come back as `std::vector<std::meta::info>`, which cannot survive into run time. `std::define_static_array` turns them into static storage, and the new **expansion statement**, `template for`, stamps out one copy of a loop body per element: ```cpp // C++26 (N5050): not compiled here; Apple clang 21 has no reflection #include <meta> #include <string_view> #include <type_traits> template <class E> requires std::is_enum_v<E> constexpr std::string_view to_string(E value) { template for (constexpr auto e : std::define_static_array(std::meta::enumerators_of(^^E))) { if (value == [:e:]) return std::meta::identifier_of(e); } return "<unknown>"; } ``` Each iteration splices one enumerator, `[:e:]`, into an ordinary comparison, and `identifier_of(e)` is a compile-time string that ends up in the binary only because the function returns it. ## What teams build with it - **Enum to string and back**, without macros or hand-kept tables. - **Serialization**: walk `nonstatic_data_members_of` to emit JSON or binary formats for any aggregate, with names from `identifier_of`. - **Struct-of-arrays** and other layouts generated from a plain struct with `define_aggregate`. - **Command-line and configuration parsers** driven by a struct's fields and annotations. - Replacing **external code generators** and X-macro tricks whose output drifts from the source. ## Reflection versus the alternatives | Approach | Strength | Weakness | |---|---|---| | X-macros and hand-kept tables | works on any compiler | duplicated lists that drift from the code | | external code generators | full control of output | an extra build step, generated code out of sight | | template tricks counting aggregate fields | no new tooling | fragile, limited to simple aggregates, no names | | C++26 static reflection | reads the real declarations, including names | needs a C++26 compiler, new skills in review | The usual first targets are the places where a list of fields or enumerators is written twice — once in the type and once in a table, a macro or a generator's input — because reflection removes the second copy. ## Costs and status Reflection moves work into the compiler, so heavy use shows up as **build time**, and it adds a new skill to code review. Access is explicit through `access_context`, so code that reads private members says so. Support in 2026 is early: GCC 16 provides it behind the experimental `-freflection` flag with `<meta>` in libstdc++ 16; the libc++ status page for LLVM 23 lists the reflection papers as not yet implemented; and Apple clang 21 rejects `^^`. The feature-test macros are `__cpp_impl_reflection` and `__cpp_lib_reflection`, both `202603L` in N5050.
- Why does a reflection loop need std::define_static_array before template for?Metafunctions such as `enumerators_of` return `std::vector<std::meta::info>`, which allocates during constant evaluation and cannot persist into the program. `std::define_static_array` copies the elements into static storage and returns a `std::span` over them, a constant that an expansion statement can iterate, instantiating its body once per reflection.
- How does C++26 reflection treat private members?Member queries take a `std::meta::access_context`. `access_context::current()` sees what the calling scope may access, `unprivileged()` sees only public members, and `unchecked()` ignores access entirely. A serializer that must read private fields therefore states `unchecked()` explicitly, which keeps encapsulation breaks visible in review rather than hidden in a macro.
- C++26 reflection stores metadata for every class in the executable.
- std::meta functions can be called with values known only at run time.
- ^^T returns the same std::type_info object as typeid(T).
- Reflection silently ignores access control on private members.
- C++26 reflection can only read declarations, never generate code.
Assumes the C++26 final draft N5050 (WG21 finished, ISO publication pending); C++23 is the latest published standard, so support varies by toolchain.