Reflection library (since C++26)
From cppreference.com
The facilities defined in this section support reflective programming, allowing C++ programs to both observe the structure of the program itself and to generate code that depends on those observations.
Contents
- 1 Core language
- 2 Reflection library
- 3 Types
- 4 Concepts
- 5 Functions
- 5.1 Promoting compile-time storage into static storage
- 5.2 Operator representations
- 5.3 Reflection names and locations
- 5.4 Reflection queries
- 5.5 Accessibility queries
- 5.6 Scope identification
- 5.7 Reflection member queries
- 5.8 Reflection layout queries
- 5.9 Annotation reflection
- 5.10 Value extraction
- 5.11 Reflection substitution
- 5.12 Expression result reflection
- 5.13 Class definition generation
- 5.14 Primary type categories
- 5.15 Composite type categories
- 5.16 Type properties
- 5.17 Type property queries
- 5.18 Type relations
- 5.19 Const-volatile modifications
- 5.20 Reference modifications
- 5.21 Sign modifications
- 5.22 Array modifications
- 5.23 Pointer modifications
- 5.24 Other transformations
- 5.25 Checking string literals
- 6 Notes
- 7 Example
Core language
The reflection library works in conjunction with two core language features:
Reflection library
Types | |
Defined in header
<meta> | |
(C++26) |
the type of reflection values (typedef) |
(C++26) |
exception thrown by reflection functions (class) |
(C++26) |
a context for access checking (class) |
(C++26) |
data member options including name, alignment, and bit width (class) |
(C++26) |
the offset of a member or base class relative to its enclosing class (class) |
(C++26) |
constants that identify overloadable operators (enum) |
Concepts | |
Defined in header
<meta> | |
(C++26) |
specifies a range of reflection values (concept) |
Functions | |
Defined in header
<meta> | |
Promoting compile-time storage into static storage | |
(C++26) |
promotes a compile-time string to static storage, returning a reflection representing the static string (function template) |
(C++26) |
promotes compile-time array into static storage, returning a reflection representing the static array (function template) |
(C++26) |
promotes compile-time string to static storage, returning a pointer to the first character of the static string (function template) |
(C++26) |
promotes compile-time array into static storage, returning a span of the static array (function template) |
(C++26) |
promotes compile-time value into static storage, returning a pointer to the static object (function template) |
Operator representations | |
(C++26) |
constants that identify overloadable operators (enum) |
(C++26) |
identifies the operator represented by the reflection (function) |
(C++26) |
converts a meta::operators enumerator to its symbolic name (function) |
Reflection names and locations | |
(C++26) |
checks if reflection represents an entity that has an identifier (function) |
(C++26) |
if the reflected entity has an identifier, obtains its name (function) |
| obtains a string suitable for identifying the reflected entity (function) | |
(C++26) |
obtains the source location of the reflected entity (function) |
Reflection queries | |
(C++26) |
determines the type of the reflected entity (function) |
(C++26) |
obtains a reflection of the object of the reflected variable (function) |
(C++26) |
obtains a reflection of the constant value of the reflected entity (function) |
| checks if reflection represents a public, protected, or private member or base class (function) | |
(C++26) |
checks if reflection represents a virtual member function or virtual base class (function) |
(C++26) |
checks if reflection represents a pure virtual member function (function) |
(C++26) |
checks if reflection represents a member function that overrides another member function (function) |
(C++26) |
checks if reflection represents a final class or final member function (function) |
(C++26) |
checks if reflection represents a deleted function (function) |
(C++26) |
checks if reflection represents a defaulted function (function) |
(C++26) |
checks if reflection represents a user-provided function (function) |
(C++26) |
checks if reflection represents a user-declared function (function) |
(C++26) |
checks if reflection represents a member function that is declared explicit (function) |
(C++26) |
checks if reflection represents a noexcept function or function type (function) |
(C++26) |
checks if reflection represents a bit-field or a data member description of bit-field (function) |
(C++26) |
checks if reflection represents an enumerator (function) |
(C++26) |
checks if reflection represents an annotation (function) |
(C++26) |
checks if reflection represents a const type or a function type with const qualifier (function) |
(C++26) |
checks if reflection represents a volatile type or a function type with volatile qualifier (function) |
(C++26) |
checks if reflection represents a mutable non-static data member (function) |
| checks if the reflected entity is a ref qualified member function (function) | |
| checks if the reflected entity has a storage duration (function) | |
| checks if the reflected entity has a linkage (function) | |
(C++26) |
checks if the reflected entity has "C" language linkage (extern "C") (function) |
(C++26) |
checks if reflection represents a complete type (function) |
(C++26) |
checks if the reflected type has its list of members completely defined (function) |
(C++26) |
checks if reflection represents a variable (function) |
(C++26) |
checks if reflection represents a type (function) |
(C++26) |
checks if reflection represents a namespace (function) |
(C++26) |
checks if reflection represents a type alias (function) |
(C++26) |
checks if reflection represents a namespace alias (function) |
(C++26) |
checks if reflection represents a function (function) |
(C++26) |
checks if reflection represents a conversion function (function) |
(C++26) |
checks if reflection represents an operator overload (function) |
(C++26) |
checks if reflection represents a user-defined literal operator (function) |
(C++26) |
checks if reflection represents a special member function (function) |
| checks if the reflected entity is a constructor of this kind (function) | |
| checks if the reflected entity is an assignment operator of this kind (function) | |
(C++26) |
checks if the reflected entity is a destructor (function) |
(C++26) |
checks if reflection represents a function parameter (function) |
(C++26) |
checks if reflection represents an explicit object parameter (function) |
(C++26) |
checks if reflected function parameter has a default argument (function) |
(C++26) |
checks if reflection represents a function or function type with an ellipsis parameter (function) |
(C++26) |
checks if reflected entity is a template (function) |
(C++26) |
checks if reflected entity is a function template (function) |
(C++26) |
checks if reflected entity is a variable template (function) |
(C++26) |
checks if reflected entity is a class template (function) |
(C++26) |
checks if reflected entity is a alias template (function) |
| checks if reflected entity is a conversion function template (function) | |
| checks if reflected entity is an operator function template (function) | |
(C++26) |
checks if reflected entity is a literal operator template (function) |
(C++26) |
checks if reflected entity is a constructor template (function) |
(C++26) |
checks if reflection represents a concept (function) |
(C++26) |
checks if reflection represents a (scalar) value (function) |
(C++26) |
checks if reflection represents a static object (function) |
(C++26) |
checks if reflection represents a structured binding (function) |
(C++26) |
checks if reflection represents a class member (function) |
(C++26) |
checks if reflection represents a namespace member (function) |
(C++26) |
checks if reflection represents a non-static data member (function) |
(C++26) |
checks if reflection represents a static class member (function) |
(C++26) |
checks if reflection represents a direct base class relationship (function) |
| checks if reflected non-static data member has a default member initializer (function) | |
(C++26) |
checks if the reflected entity is contained in a scope (function) |
(C++26) |
obtains the scope that contains the reflected entity (function) |
(C++26) |
determines the underlying entity of the reflected type/namespace alias (function) |
(C++26) |
checks if reflected entity is produced by substituting template arguments into a template (function) |
(C++26) |
determines the template used to instantiate the reflected entity (function) |
(C++26) |
obtains the template arguments of the reflected entity (function) |
(C++26) |
obtains the parameters of the reflected function (function) |
(C++26) |
obtains the variable of the reflected function parameter in the function definition (function) |
(C++26) |
obtains the return type of the reflected function (function) |
Accessibility queries | |
(C++26) |
checks if a member is accessible in a given context (function) |
| checks if the reflected class has any inaccessible non-static data members (function) | |
(C++26) |
checks if the reflected class has any inaccessible direct base classes (function) |
(C++26) |
checks if the reflected class has any inaccessible non-static data members or direct base classes (function) |
Scope identification | |
(C++26) |
returns a reflection of the enclosing function (function) |
(C++26) |
returns a reflection of the enclosing class (function) |
(C++26) |
returns a reflection of the enclosing namespace (function) |
Reflection member queries | |
(C++26) |
obtains the accessible direct members of the reflected class or namespace (function) |
(C++26) |
obtains the accessible direct bases of the reflected class (function) |
(C++26) |
obtains the accessible static data members of the reflected class (function) |
(C++26) |
obtains the accessible non-static data members of the reflected class (function) |
(C++26) |
obtains the accessible base class subobjects and non-static data members of the reflected class (function) |
(C++26) |
obtains the enumerators of the reflected enum type (function) |
Reflection layout queries | |
(C++26) |
returns the offset of a (member or base) subobject relative to its enclosing class (function) |
(C++26) |
returns the size in bytes of the reflected object or type (function) |
(C++26) |
returns the alignment of the reflected object or type (function) |
(C++26) |
returns the size in bits of the reflected object or type (function) |
Annotation reflection | |
(C++26) |
obtains the annotations applying to the reflected entity (function) |
(C++26) |
obtains the annotations that apply to the reflected entity and have the specified type (function) |
Value extraction | |
(C++26) |
extracts the value of the reflected entity (function template) |
Reflection substitution | |
(C++26) |
checks whether the reflected arguments can be substituted in the reflected template (function template) |
(C++26) |
substitutes reflected arguments in the reflected template (function template) |
Expression result reflection | |
(C++26) |
returns a reflection representing a value or template parameter object, suitable for use as a constant template argument (function template) |
(C++26) |
returns a reflection representing an object, suitable for use as a constant template argument (function template) |
(C++26) |
returns a reflection representing a function, suitable for use as a constant template argument (function template) |
Class definition generation | |
(C++26) |
creates a data member description for use with injected declarations (function) |
(C++26) |
checks if reflection represents a data member description (function) |
(C++26) |
defines an aggregate type from a sequence of data member descriptions (function template) |
Primary type categories | |
(C++26) |
checks if reflected type is void (function) |
(C++26) |
checks if reflected type is std::nullptr_t (function) |
(C++26) |
checks if reflected type is an integral type (function) |
(C++26) |
checks if reflected type is a floating-point type (function) |
(C++26) |
checks if reflected type is an array type (function) |
(C++26) |
checks if reflected type is a pointer type (function) |
(C++26) |
checks if reflection represents an lvalue reference (function) |
(C++26) |
checks if reflection represents an rvalue reference (function) |
| checks if reflection represents a member object pointer type (function) | |
| checks if reflection represents a member function pointer type (function) | |
(C++26) |
checks if reflected type is an enumeration type (function) |
(C++26) |
checks if reflected type is a union type (function) |
(C++26) |
checks if reflected type is a non-union class type (function) |
(C++26) |
checks if reflected type is a function type (function) |
(C++26) |
checks if reflected type is std::meta::info (function) |
Composite type categories | |
(C++26) |
checks if reflection represents either an lvalue reference or rvalue reference (function) |
(C++26) |
checks if reflection represents an arithmetic type (function) |
(C++26) |
checks if reflection represents a fundamental type (function) |
(C++26) |
checks if reflected type is an object type (function) |
(C++26) |
checks if reflected type is a scalar type (function) |
(C++26) |
checks if reflected type is a compound type (function) |
(C++26) |
checks if reflection represents a member pointer type (function) |
Type properties | |
(C++26) |
checks if reflection represents a const-qualified type (function) |
(C++26) |
checks if reflection represents a volatile-qualified type (function) |
(C++26) |
checks if reflection represents a trivially copyable type (function) |
(C++26) |
checks if reflected type is a standard-layout type (function) |
(C++26) |
checks if reflected type is a class (but not union) type and has no non-static data members (function) |
(C++26) |
checks if reflected type is a polymorphic class type (function) |
(C++26) |
checks if reflected type is an abstract class type (function) |
(C++26) |
checks if reflected type is a final class type (function) |
(C++26) |
checks if reflected type is an aggregate type (function) |
(C++26) |
checks if reflected type is a structural type (function) |
(C++26) |
checks if reflection represents a signed arithmetic type (function) |
(C++26) |
checks if reflection represents an unsigned arithmetic type (function) |
(C++26) |
checks if reflection represents an array type of known bound (function) |
(C++26) |
checks if reflection represents an array type of unknown bound (function) |
(C++26) |
checks if reflection represents a scoped enumeration type (function) |
| checks if reflected type has a constructor for specific arguments (function template) | |
| checks if reflected type has a default constructor (function) | |
| checks if reflected type has a copy constructor (function) | |
| checks if reflected type can be constructed from an rvalue reference (function) | |
| checks if reflected type has an assignment operator for a specific argument (function) | |
| checks if reflected type has a copy assignment operator (function) | |
| checks if reflected type has a move assignment operator (function) | |
| checks if objects of one reflected type can be swapped with objects of same or different other reflected type (function) | |
| checks if reflected type has a non-deleted destructor (function) | |
(C++26) |
checks if reflected type is an implicit-lifetime type (function) |
(C++26) |
checks if reflected type has a virtual destructor (function) |
| checks if every bit in reflected type's object representation contributes to its value (function) | |
| checks if a reference is bound to a temporary in direct-initialization (function) | |
| checks if a reference is bound to a temporary in copy-initialization (function) | |
Type property queries | |
(C++26) |
obtains the number of dimensions of reflected array type (function) |
(C++26) |
obtains the size of reflected array type along a specified dimension (function) |
Type relations | |
(C++26) |
checks if two reflections represent the same types (function) |
(C++26) |
checks if two reflections represent types such that one type is a base of the other type (function) |
(C++26) |
checks if two reflections represent types such that one type is a virtual base of the other type (function) |
| checks if reflected type can be converted to the other type (function) | |
(C++26) |
checks if two reflections represent layout-compatible types (function) |
| checks if reflected type is a pointer-interconvertible base of another type (function) | |
| checks if the reflected type can be invoked (as if by std::invoke) with the given argument types (function template) | |
Const-volatile modifications | |
removes const and/or volatile specifiers from reflected type (function) | |
(C++26) |
adds const and/or volatile specifiers to reflected type (function) |
Reference modifications | |
(C++26) |
removes a reference from reflected type (function) |
| adds an lvalue or rvalue reference to reflected type (function) | |
Sign modifications | |
(C++26) |
obtains the corresponding signed type for reflected integral type (function) |
(C++26) |
obtains the corresponding unsigned type for reflected integral type (function) |
Array modifications | |
(C++26) |
removes one extent from reflected array type (function) |
(C++26) |
removes all extents from reflected array type (function) |
Pointer modifications | |
(C++26) |
removes a pointer from reflected type (function) |
(C++26) |
adds a pointer to reflected type (function) |
Other transformations | |
(C++26) |
combines meta::remove_cv and meta::remove_reference (function) |
(C++26) |
applies type transformations as when passing a function argument by value (function) |
(C++26) |
determines the common type of a sequence of reflected types (function template) |
(C++26) |
determines the common reference type of a sequence of reflected types (function template) |
(C++26) |
obtains the underlying integer type for reflected enumeration type (function) |
(C++26) |
deduces the result type of invoking a callable object with a set of arguments (function template) |
| get the reference type wrapped in std::reference_wrapper (function) | |
(C++26) |
obtains the size of reflected tuple (function) |
(C++26) |
obtains the type of the specified element of reflected tuple (function) |
| checks if the callable object can be invoked (as if by std::invoke) with a tuple of arguments (function) | |
(C++26) |
deduces the result type of invoking a callable object with a tuple of arguments (function) |
(C++26) |
obtains the size of reflected variant's list of alternatives (function) |
(C++26) |
obtains the type of reflected tuple's alternative specified by its index (function) |
(C++26) |
performs three-way comparison on reflected types (function) |
Checking string literals | |
(C++26) |
checks if a pointer points into a string literal (function) |
Notes
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_reflection |
202506L |
(C++26) | <meta>: Reflection library support. Constexpr exceptions as error handling in reflection. |
Example
| This section is incomplete Reason: no example |