Reflection & metaprogramming

From type traits and SFINAE to ^^ reflection and splicing: asking the compiler about types, members and enumerators.

Drill points
  • ^^x produces a std::meta::info; [: r :] splices it back into code
  • Why std::meta::info values exist only during constant evaluation
  • Looping over members and enumerators with template for and define_static_array
  • Annotations ([[=value]]) as user metadata read through reflection
  • What reflection still cannot do: generate new declarations (the C++29 direction)

Introspection means <type_traits>: is_same, is_integral, decltype and declval let templates ask questions about types, and enable_if turns the answers into overload selection. Only the questions the library standardized can be asked: members, names and enumerators stay invisible.

template <class T>
typename std::enable_if<std::is_trivially_copyable<T>::value>::type
save(const T& v, Sink& out) { out.write(&v, sizeof v); }   // memcpy-safe types only

_t aliases trim the typename ...::type noise, and std::index_sequence becomes the backbone of tuple-walking metaprograms, still expressed through pack-expansion tricks.

template <class Tuple, std::size_t... I>
void print_all(const Tuple& t, std::index_sequence<I...>) {
  int expand[] = {0, ((std::cout << std::get<I>(t) << ' '), 0)...};
  (void)expand;
}

if constexpr, fold expressions, void_t detection and the _v shorthands make compile-time code read like code. Structured bindings expose aggregate members by position, which libraries such as Boost.PFR stretch into reflection of plain structs.

template <class... Ts>
auto sum(Ts... xs) { return (xs + ... + 0); }      // fold expression

template <class T>
void describe(const T& v) {
  if constexpr (std::is_enum_v<T>) print_enum(v);
  else                             print_value(v);
}

Concepts replace detection idioms, and consteval plus constexpr containers let metaprograms be ordinary functions. There is still no way to ask for a type's members or an enumerator's name: enum-to-string remains a macro, or a compiler-specific __PRETTY_FUNCTION__ parsing hack as in magic_enum.

template <class T>
concept Serializable = requires(const T& t, Archive& a) { t.serialize(a); };

consteval std::size_t table_size(std::size_t n) { return std::bit_ceil(n); }

Little changes for introspection itself (if consteval, std::to_underlying, more of the library constexpr). The reflection effort regroups: the type-based Reflection TS (reflexpr, 2021) gives way to a value-based redesign, P2996, aimed at C++26.

Static reflection (P2996, adopted at Sofia 2025): ^^x yields a std::meta::info, consteval functions in <meta> query it (members_of, identifier_of, enumerators_of), and [: r :] splices it back into code. With expansion statements (template for), annotations and define_static_array, enum-to-string and member-wise serialization become plain library code.

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 "<unnamed>";
}

template <class T>
void dump(const T& obj) {
  constexpr auto ctx = std::meta::access_context::current();
  template for (constexpr auto m :
                std::define_static_array(std::meta::nonstatic_data_members_of(^^T, ctx)))
    std::println("{} = {}", std::meta::identifier_of(m), obj.[:m:]);
}
C++29direction · not adopted

Direction: code generation. Token injection (the P3294 line of work) would let reflection write declarations, not just read them, which is what generated members, bindings and builders need; richer queries (attributes, expressions) are follow-ups.

C++26 per the final working draft; C++29 rows are proposals and direction, not adopted features.