Skip to main content

Practical Reflection With C++26 - Barry Revzin - CppCon 2025

[HPP] Katie LedeckyFebruary 18, 20261h 7min
28 connections·40 entities in this video

C++26 Reflection Overview

  • 💡 The adoption of Reflection for C++26 significantly expands what's possible in C++ programming.
  • 🎯 This talk demonstrates reflection by implementing a Struct-of-Arrays (SOA) vector for arbitrary aggregate types.
  • 🔑 The primary goal of reflection is to enable the creation of easy-to-use libraries, such as an SOA vector that "just works" without complex user code.

Core Reflection Mechanisms

  • 🛠️ C++26 reflection provides a narrow code generation mechanism to populate incomplete types with specific non-static data members.
  • 🚀 This is achieved using a consteval block and calling define_aggregate with a reflection representing the incomplete type.
  • 🧠 The substitute and extract pattern is crucial for working with templates and their values, allowing for template instantiation and value retrieval at compile time.

Building the SOA Vector

  • ✅ An SOA vector requires the type T to be an aggregate, have independently destructible members, and ideally no base classes.
  • 📝 Type traits can now be written as regular functions (e.g., is_aggregate_type) instead of only templates, simplifying constraint checks.
  • ⚡ Implementing push_back involves iterating over members using structured bindings and new C++26 features like expansion statements and views::indices.
  • 🧩 The define_static_array function promotes a range to static storage, returning a span, which is useful for compile-time array creation.

Enhancing Functionality with Annotations

  • Annotations (e.g., derived_debug, format_as) are a new C++26 feature allowing users to attach metadata to types.
  • 🔍 These annotations can drive custom behaviors, such as automatic formatting for debugging purposes, by specializing formatters.
  • 💬 The has_annotation function checks for specific annotations, and identifier_of vs. display_string_of handle different naming contexts.
  • ⚠️ Compile-time exceptions using meta_exception can provide user-friendly error messages during code generation.

Future of C++ with Reflection

  • 📈 Reflection opens up possibilities for creating highly generic and powerful libraries that were previously impossible or overly complex.
  • 💡 It blurs the distinction between language features and library features, potentially allowing more features to start as libraries for broader experimentation.
  • 🚀 Examples like the with function, which selects members based on parameter names, demonstrate how reflection enables more intuitive and less verbose APIs.
Knowledge graph40 entities · 28 connections

How they connect

An interactive map of every person, idea, and reference from this conversation. Hover to trace connections, click to explore.

Hover · drag to explore
40 entities
Chapters17 moments

Key Moments

Transcript252 segments

Full Transcript

Topics17 themes

What’s Discussed

C++26ReflectionStruct-of-Arrays (SOA)SOA vectorAggregate typesdefine_aggregateconstevalsubstitute and extractType traitsStructured bindingsExpansion statementsdefine_static_arrayAnnotationsCompile-time exceptionsformatterMeta-programmingCode generation
Smart Objects40 · 28 links
People· 3
Concepts· 25
Products· 10
Company· 1
Event· 1