A progressive, example-driven introduction to static reflection in C++26.
The goal is not to memorize new punctuation. It is to understand the problem reflection solves, the compile-time model it introduces, and the boundary between reflection and ordinary runtime code.
Prefer one continuous introduction? Read C++26 Reflection, Made Simple.
- Why C++ needed reflection
- The compile-time mental model
- Reflect, query, and splice
- Enum names without duplicate lists
- Member-wise serialization
- Limits, costs, and sharp edges
- Compiler support
This repository intentionally separates its source code:
examples/portable/contains C++20 programs that demonstrate the old duplication and registration problems. They build on ordinary compilers.examples/cpp26/contains the corresponding C++26 reflection programs. They are reference syntax examples for a special experimental toolchain. Ordinary released compilers generally cannot include<meta>, so these files are excluded from the default build.
That separation matters. A beginner should be able to run the starting point without being blocked by an unfinished toolchain.
Requirements:
- CMake 3.25 or newer
- A C++20 compiler
cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failureRun either example directly:
./build/manual_enum
./build/manual_fieldsDo not expect this build to work with a normal compiler installation: <meta>
is generally not available there yet. Only enable this option after separately
installing an experimental compiler and standard library that support <meta>,
the ^^ reflection operator, splicing, std::define_static_array, and expansion
statements:
cmake -S . -B build-reflection \
-DBUILD_CPP26_REFLECTION_EXAMPLES=ON
cmake --build build-reflectionWithout installing that toolchain, use the repository's online check. It sends
the three files in examples/cpp26/ to Compiler Explorer's Bloomberg
Clang/P2996 compiler, then runs them:
python3 tools/check_cpp26_reflection.pyThe same command is available in VS Code as the
C++26 reflection: check online task. It requires Python 3, curl, and network
access. Do not use it for private source code.
A compiler accepting -std=c++2c or -std=c++26 does not necessarily implement
reflection yet. See compiler support.
C++26 reflection lets compile-time C++ inspect declarations and turn the resulting metadata back into type-checked C++ constructs.
The short model is:
C++ declaration -- reflect --> metadata -- query/transform --> metadata
C++ construct <-- splice -- metadata
docs/ Progressive lessons
examples/portable/ Runnable C++20 "before" examples
examples/cpp26/ C++26 reflection counterparts
tools/ Reflection validation scripts
This repository teaches the adopted model but is not a replacement for the standard wording.