Skip to content

Latest commit

 

History

History
113 lines (82 loc) · 3.55 KB

README.md

File metadata and controls

113 lines (82 loc) · 3.55 KB

C++26 Reflection for Beginners

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.

Start Here

  1. Why C++ needed reflection
  2. The compile-time mental model
  3. Reflect, query, and splice
  4. Enum names without duplicate lists
  5. Member-wise serialization
  6. Limits, costs, and sharp edges
  7. Compiler support

Two Kinds of Examples

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.

Build the Portable Examples

Requirements:

  • CMake 3.25 or newer
  • A C++20 compiler
cmake -S . -B build
cmake --build build
ctest --test-dir build --output-on-failure

Run either example directly:

./build/manual_enum
./build/manual_fields

C++26 Build

Do 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-reflection

Without 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.py

The 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.

The One-Sentence Definition

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

Repository Map

docs/               Progressive lessons
examples/portable/  Runnable C++20 "before" examples
examples/cpp26/     C++26 reflection counterparts
tools/               Reflection validation scripts

Standardization References

This repository teaches the adopted model but is not a replacement for the standard wording.