Metaprogramming library
A guide to Chapter 21: compile-time integer sequences, <type_traits>, C++26 static reflection in <meta>, and compile-time rationals in <ratio>, plus how the two generations of metaprogramming interfaces differ.
Key points
- Everything in Chapter 21 works at compile time: querying properties of types, turning one type into another, carrying integers and rationals in types, and C++26 static reflection.
<type_traits>is a class template interface: template arguments are the input, and::valueor::typeis the output.- C++26's
<meta>is a value interface: the reflection valuestd::meta::infois input and output, and queries are ordinaryconstevalfunctions. - Most traits just turn definitions that already exist in the core language (trivially copyable, aggregate, convertible, ...) into something code can query, so understanding a trait starts with understanding the corresponding core rule.
Four parts#
¶1 The chapter consists of four subclauses:
| Subclause | Header | Contents |
|---|---|---|
| [intseq] | <utility> |
integer_sequence, make_index_sequence: encoding 0..N-1 into a type for a function template to deduce as a parameter pack |
| [type.traits] | <type_traits> |
Type categories, type properties, type relationships, type transformations, logical composition, member relationships, the constant evaluation context |
| [meta.reflection] | <meta> |
C++26 The static reflection library: queries on std::meta::info, member enumeration, substitute, define_aggregate |
| [ratio] | <ratio> |
std::ratio<N, D>: exact compile-time rationals, on which the units of <chrono> are built |
integer_sequence is declared in <utility>, not <type_traits>. The first line of the synopses of <type_traits> and <ratio> reads // all freestanding: a freestanding implementation (one that does not depend on an operating system; see freestanding implementation) must provide them in full too, because they need no run-time support at all.
Two generations of interface#
Two generations of design sit side by side in this chapter.
The first generation is <type_traits>, using class templates as compile-time functions. The input of std::is_trivially_copyable<T> is the template argument, and the output is the static member value in the base class integral_constant<bool, v>; the output of std::remove_cv<T> is the nested type type. Branching is done with partial specialization or SFINAE, looping with recursive instantiation. The [type.traits] page covers the three standard shapes of this interface.
The second generation is C++26's <meta>. std::meta::info is a scalar type ([basic.fundamental]), ^^T produces a value of it ([expr.reflect]), and [: r :] splices it back into code ([basic.splice]). The functions in <meta> are all ordinary consteval functions taking info and returning bool, info or vector<info>, and can be processed with for loops and ranges algorithms. [meta.reflection.traits] also provides a corresponding function for each first-generation trait.
// first generation: types in, a type or bool out
static_assert(std::is_same_v<std::remove_cvref_t<const int&>, int>);
// second generation: reflection values in, reflection values out
static_assert(std::meta::remove_cvref(^^const int&) == ^^int);The second assertion has not been compiled: Clang 23 does not support reflection, and libc++ has no <meta> header.
One generation does not replace the other. Most <type_traits> traits are thin wrappers around compiler builtins, cheap to instantiate, and remain the workhorse of everyday generic code. Reflection solves problems traits cannot express: enumerating the members of a class, reading the names of declarations, generating a new class from a description.
Traits restate the core language#
This chapter rarely defines new concepts. Most traits turn some definition in the core language into a queryable interface:
| Library component | Corresponding core rule |
|---|---|
is_trivially_copyable, is_standard_layout, is_implicit_lifetime |
[basic.types.general]/7, [class.prop] |
is_aggregate |
[dcl.init.aggr] |
is_structural |
Structural types in [temp.param] |
is_layout_compatible, is_pointer_interconvertible_base_of |
Layout compatibility in [basic.types], pointer-interconvertibility in [basic.compound] |
is_constructible, is_convertible |
Whether a hypothetical variable definition or return statement is well-formed ([dcl.init], [stmt.return]) |
common_type |
The result type of the conditional operator ?: ([expr.cond]) |
reference_constructs_from_temporary |
Reference binding and lifetime extension of temporaries ([class.temporary]) |
is_constant_evaluated, is_within_lifetime |
Constant evaluation ([expr.const]) and lifetime ([basic.life]) |
std::meta::* |
[expr.reflect], [basic.splice] |
So when reading a trait, first pin down the core definition it corresponds to, then look at where the library wording departs from intuition. For example, is_signed<E> is always false for an enumeration, and on Clang is_nothrow_constructible also depends on the destructor.
Evolution#
History
- C++11 adopted the type traits from TR1, establishing the interface of an
integral_constantbase plus::valueand::type, and added<ratio>,declval,common_typeand theis_constructiblefamily that depends on new language features. - C++14 added the
_talias templates (remove_cv_t<T>) andinteger_sequence. - C++17 added the
_vvariable templates,void_t,conjunction/disjunction/negation,is_aggregateandhas_unique_object_representations, and replacedresult_ofwithis_invocable/invoke_result. - C++20 added
remove_cvref,type_identity,common_reference,is_constant_evaluated, and the layout-compatibility and pointer-interconvertibility families, and deprecatedis_pod. - C++23 added
is_scoped_enum,reference_constructs_from_temporaryandis_implicit_lifetime. - C++26 added
is_virtual_base_of,is_within_lifetimeand static reflection in<meta>(P2996R13), deprecatedis_trivial(P3247R2), and gaveinteger_sequencestructured binding support.
Page index#
- [intseq]:
integer_sequence,make_integer_sequence, and C++26 structured binding support; - [type.traits]: the three shapes of a trait, the no-specialization and incomplete-type rules,
integral_constant; - [meta.unary]: primary type categories, composite type categories,
rank/extent/alignment_of; - [meta.unary.prop]: type properties, including the precise definition of
is_constructibleand its trivial and nothrow variants; - [meta.rel]:
is_same,is_base_of,is_convertible,is_invocable, layout compatibility; - [meta.trans] and [meta.trans.other]: the type transformations,
decay,common_type,common_reference; - [meta.logical], [meta.member], [meta.const.eval];
- [meta.reflection] and its subpages: the C++26 reflection library;
- [ratio].
Contents
- 21.1General[meta.general]
- 21.2Compile-time integer sequences[intseq]done
- 21.3Metaprogramming and type traits[type.traits]done
- 21.3.1General[type.traits.general]
- 21.3.2Requirements[meta.rqmts]
- 21.3.3Header <type_traits> synopsis[meta.type.synop]
- 21.3.4Helper classes[meta.help]
- 21.3.5Unary type traits[meta.unary]done
- 21.3.6Type property queries[meta.unary.prop.query]
- 21.3.7Relationships between types[meta.rel]draft
- 21.3.8Transformations between types[meta.trans]draft
- 21.3.8.1General[meta.trans.general]
- 21.3.8.2Const-volatile modifications[meta.trans.cv]
- 21.3.8.3Reference modifications[meta.trans.ref]
- 21.3.8.4Sign modifications[meta.trans.sign]
- 21.3.8.5Array modifications[meta.trans.arr]
- 21.3.8.6Pointer modifications[meta.trans.ptr]
- 21.3.8.7Other transformations[meta.trans.other]draft
- 21.3.9Logical operator traits[meta.logical]done
- 21.3.10Member relationships[meta.member]draft
- 21.3.11Constant evaluation context[meta.const.eval]draft
- 21.4Reflection[meta.reflection]draft
- 21.4.1Header <meta> synopsis[meta.syn]
- 21.4.2Checking string literals[meta.string.literal]
- 21.4.3Promoting to static storage[meta.define.static]
- 21.4.4Class exception[meta.reflection.exception]
- 21.4.5Operator representations[meta.reflection.operators]
- 21.4.6Reflection names and locations[meta.reflection.names]
- 21.4.7Reflection queries[meta.reflection.queries]draft
- 21.4.8Scope identification[meta.reflection.scope]
- 21.4.9Access control context[meta.reflection.access.context]
- 21.4.10Member accessibility queries[meta.reflection.access.queries]
- 21.4.11Reflection member queries[meta.reflection.member.queries]
- 21.4.12Reflection layout queries[meta.reflection.layout]
- 21.4.13Annotation reflection[meta.reflection.annotation]
- 21.4.14Value extraction[meta.reflection.extract]draft
- 21.4.15Reflection substitution[meta.reflection.substitute]
- 21.4.16Expression result reflection[meta.reflection.result]
- 21.4.17Reflection class definition generation[meta.reflection.define.aggregate]
- 21.4.18Reflection type traits[meta.reflection.traits]draft
- 21.5Compile-time rational arithmetic[ratio]done