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
constevalblock and callingdefine_aggregatewith a reflection representing the incomplete type. - π§ The
substituteandextractpattern 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
Tto 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_backinvolves iterating over members using structured bindings and new C++26 features like expansion statements andviews::indices. - π§© The
define_static_arrayfunction 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_annotationfunction checks for specific annotations, andidentifier_ofvs.display_string_ofhandle different naming contexts. - β οΈ Compile-time exceptions using
meta_exceptioncan 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
withfunction, 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