Document Number: | P0670R3, ISO/IEC JTC1 SC22 WG21 |
Audience: | CWG, LWG |
Date: | 2018-05-07 |
Authors: | Matúš Chochlík (chochlik@gmail.com) |
Axel Naumann (axel@cern.ch) | |
David Sankel (camior@gmail.com) |
P0194 introduced static reflection for types and variables. This paper adds static reflection of functions.
Reflection proposed here behaves as follows:
void func(int);
void func(std::string);
using func_call_m = reflexpr(func(123));
using func_m = get_callable_t<func_call_m>; // reflects void func(int)
using param0_m = get_element_t<0, get_parameters_t<func_m>>;
cout << get_name_v<get_type_t<param0_m>> << '\n'; // prints "int"
The functionality introduced here allows for reflection of calls of concrete functions. This enables, for instance, GUI generation, building a catalogue for remote procedure calls, documentation generation, and signal/slot frameworks. Like P0194, this proposal omits attributes, templates, and reification (i.e. conversion of a meta object to the base level): all warrant a separate paper. We would welcome a paper especially on static reflection of attributes, matching the interface-style of P0194 and this paper! Linkage and friends will be part of a follow-up paper to P0194; they will have a combined "effect" on P0194 and this paper.
Most notably, this proposal relies on the Reflection TS and the Concepts TS.
P0385 discusses use cases, rationale, design decisions, and the future evolution of the proposed reflection facility. It also has usage examples and replies to frequently asked questions.
This document is written as a set of changes against the Reflection TS (N4746).
Instructions to modify or add paragraphs are written as explicit instructions.
Modifications made directly to existing text from the Reflection TS use underlining to represent added text and strikethrough to represent deleted text.
No changes are made to Clause 2 of the Reflection TS.
No changes are made to Clause 3 of the Reflection TS.
Modify the section as follows:
This work is the result of a collaboration of researchers in industry and academia. We wish to thank people who made valuable contributions within and outside these groups, including Ricardo Fabiano de Andrade, Roland Bock, Chandler Carruth, Jackie Kay, Klaim-Joël Lamotte, Jens Maurer, and many others not named here who contributed to the discussion.
No changes are made to Clause 5 of the Reflection TS.
No changes are made to Clause 6 of the Reflection TS.
No changes are made to Clause 7 of the Reflection TS.
No changes are made to Clause 8 of the Reflection TS.
No changes are made to Clause 9 of the Reflection TS.
In C++ [dcl.type.simple], apply the following change
::
Apply the following modification to the enumeration:
A
is not the global namespace and B
is an enclosing namespace of A
,A
is a lambda capture of the closure type B
,A
is the closure type of the lambda capture B
,A
is the return type, parameter type, or function type of the function B
, orA
is reflection-related to an entity or alias X
and X
is reflection-related to B
.For the operand ::
, the type specified by the reflexpr-specifier
satisfies reflect::GlobalScope
.
For an operand that can be parsed as a function call expression [expr.call], the type satisfies reflect::FunctionCallExpression
.
For an operand of the form identifier where identifier is a template type-parameter, the type satisfies both reflect::Type
and reflect::Alias
.
Modify Table 12 as follows:
Category | identifier or simple-template-id kind | reflect Concept |
---|---|---|
type | class-name designating a union | reflect::Record |
class-name designating a closure type | reflect::Lambda | |
class-name designating a non-union class | reflect::Class | |
enum-name | reflect::Enum | |
type-name introduced by a using-declaration | both reflect::Type and reflect::Alias | |
any other typedef-name | both reflect::Type and reflect::Alias | |
namespace | namespace-alias | both reflect::Namespace and reflect::Alias |
any other namespace-name | both reflect::Namespace and reflect::ScopeMember | |
data member | the name of a data member | reflect::Variable |
value | the name of a variable or structured binding that is not a local entity | reflect::Variable |
the name of an enumerator | both reflect::Enumerator and reflect::Constant | |
the name of a function parameter | reflect::FunctionParameter | |
the name of a captured entity [expr.prim.lambda.capture] | reflect::LambdaCapture |
Modify the following paragraph as follows:
If the reflexpr-operand designates an entity or alias at block scope (6.3.3) or function prototype scope (6.3.4) and the entity is not captured or a function parameter, the program is ill-formed.
If the reflexpr-operand designates a class member, the type represented by the reflexpr-specifier also satisfies reflect::RecordMember
.
If the reflexpr-operand designates an variable or a data member, a function parameter, or a captured entity, it is an unevaluated operand (expr.context).
If the reflexpr-operand designates both an alias and a class name, the type represented by the reflexpr-specifier reflects the alias and satisfies Alias
.
If the overload resolution of the function call expression is ambiguous, the programm is ill-formed.
No changes are made to Clause 11 of the Reflection TS.
No changes are made to Clause 13 of the Reflection TS.
No changes are made to Clause 14 of the Reflection TS.
No changes are made to Clause 15 of the Reflection TS.
No changes are made to Clause 16 of the Reflection TS.
No changes are made to Clause 17 of the Reflection TS.
No changes are made to Clause 18 of the Reflection TS.
No changes are made to Clause 19 of the Reflection TS.
No changes are made to Clause 20 of the Reflection TS.
<experimental/reflect>
synopsis [reflect.synopsis]
namespace std::experimental::reflect {
inline namespace v1 {
// 21.11.3 Concepts for meta-object types
template <class T> concept Object;
template <class T> concept ObjectSequence;
template <class T> concept Named;
template <class T> concept Alias;
template <class T> concept RecordMember;
template <class T> concept Enumerator;
template <class T> concept Variable;
template <class T> concept ScopeMember;
template <class T> concept Typed;
template <class T> concept Namespace;
template <class T> concept GlobalScope;
template <class T> concept Class;
template <class T> concept Enum;
template <class T> concept Record;
template <class T> concept Scope;
template <class T> concept Type;
template <class T> concept Constant;
template <class T> concept Base;
template <class T> concept FunctionParameter;
template <class T> concept Callable;
template <class T> concept FunctionCallExpression;
template <class T> concept Function;
template <class T> concept RecordMemberFunction;
template <class T> concept SpecialMemberFunction;
template <class T> concept Constructor;
template <class T> concept Destructor;
template <class T> concept ConversionOperator;
template <class T> concept Lambda;
template <class T> concept LambdaCapture;
// 21.11.4 Meta-object operations
// 21.11.4.1 Multi-concept operations
template <class T> struct is_public;
template <class T> struct is_protected;
template <class T> struct is_private;
template <class T> struct is_constexpr;
template <class T> struct is_static;
template <class T> struct is_final;
template <class T> struct get_pointer;
template <class T> struct is_explicit;
template <class T> struct is_inline;
template <class T> struct is_virtual;
template <class T> struct is_pure_virtual;
template <class T>
constexpr auto is_public_v = is_public<T>::value;
template <class T>
constexpr auto is_protected_v = is_protected<T>::value;
template <class T>
constexpr auto is_private_v = is_private<T>::value;
template <class T>
constexpr auto is_constexpr_v = is_constexpr<T>::value;
template <class T>
constexpr auto is_static_v = is_static<T>::value;
template <class T>
constexpr auto is_final_v = is_final<T>::value;
template <class T>
constexpr auto get_pointer_v = get_pointer<T>::value;
template <class T>
constexpr auto is_explicit_v = is_explicit<T>::value;
template <class T>
constexpr auto is_inline_v = is_inline<T>::value;
template <class T>
constexpr auto is_virtual_v = is_virtual<T>::value;
template <class T>
constexpr auto is_pure_virtual_v = is_pure_virtual<T>::value;
// 21.11.4.8 Record operations
template <Record T> struct get_public_data_members;
template <Record T> struct get_accessible_data_members;
template <Record T> struct get_data_members;
template <Record T> struct get_public_member_functions;
template <Record T> struct get_accessible_member_functions;
template <Record T> struct get_member_functions;
template <Record T> struct get_public_member_types;
template <Record T> struct get_accessible_member_types;
template <Record T> struct get_member_types;
template <Record T> struct get_constructors;
template <Record T> struct get_destructors;
template <Record T> struct get_operators;
template <Class T> struct get_public_base_classes;
template <Class T> struct get_accessible_base_classes;
template <Class T> struct get_base_classes;
template <Class T> struct is_final<T>;
template <Record T>
using get_public_data_members_t = typename get_public_data_members<T>::type;
template <Record T>
using get_accessible_data_members_t = typename get_accessible_data_members<T>::type;
template <Record T>
using get_data_members_t = typename get_data_members<T>::type;
template <Record T>
using get_public_member_functions_t = typename get_public_member_functions<T>::type;
template <Record T>
using get_accessible_member_functions_t = typename get_accessible_member_functions<T>::type;
template <Record T>
using get_member_functions_t = typename get_member_functions<T>::type;
template <Record T>
using get_public_member_types_t = typename get_public_member_types<T>::type;
template <Record T>
using get_accessible_member_types_t = typename get_accessible_member_types<T>::type;
template <Record T>
using get_member_types_t = typename get_member_types<T>::type;
template <Record T>
using get_constructors_t = typename get_constructors<T>::type;
template <Record T>
using get_destructors_t = typename get_destructors<T>::type;
template <Record T>
using get_operators_t = typename get_operators<T>::type;
template <Class T>
using get_public_base_classes_t = typename get_public_base_classes<T>::type;
template <Class T>
using get_accessible_base_classes_t = typename get_accessible_base_classes<T>::type;
template <Class T>
using get_base_classes_t = typename get_base_classes<T>::type;
template <Class T>
constexpr auto is_final_v = is_final<T>::value;
// 21.11.4.10 Value operations
template <Constant T> struct get_constant;
template <Variable T> struct is_constexpr<T>;
template <Variable T> struct is_static<T>;
template <Variable T> struct get_pointer<T>;
template <Constant T>
constexpr auto get_constant_v = get_constant<T>::value;
template <Variable T>
constexpr auto is_constexpr_v = is_constexpr<T>::value;
template <Variable T>
constexpr auto is_static_v = is_static<T>::value;
template <Variable T>
const auto get_pointer_v = get_pointer<T>::value;
// 21.11.4.11 Base operations
template <Base T> struct get_class;
template <Base T> struct is_virtual<T>;
template <Base T> struct is_public<T>;
template <Base T> struct is_protected<T>;
template <Base T> struct is_private<T>;
template <Base T>
using get_class_t = typename get_class<T>::type;
template <Base T>
constexpr auto is_virtual_v = is_virtual<T>::value;
// 21.11.4.12 Namespace operations
template <Namespace T> struct is_inline<T>;
template <Namespace T>
constexpr auto is_inline_v = is_inline<T>::value;
// 21.11.4.13 FunctionParameter operations
template <FunctionParameter T> struct is_ellipsis;
template <FunctionParameter T> struct has_default_value;
template <FunctionParameter T>
constexpr auto is_ellipsis_v = is_ellipsis<T>::value;
template <FunctionParameter T>
constexpr auto has_default_value_v = has_default_value<T>::value;
// 21.11.4.14 Callable operations
template <Callable T> struct get_parameters;
template <Callable T> struct is_constexpr<T>;
template <Callable T> struct is_noexcept<T>;
template <Callable T> struct is_inline<T>;
template <Callable T> struct is_deleted;
template <Callable T>
using get_parameters_t = typename get_parameters<T>::type;
template <Callable T>
constexpr auto is_deleted_v = is_deleted<T>::value;
// 21.11.4.15 FunctionCallExpression operations
template <FunctionCallExpression T> struct get_callable;
template <FunctionCallExpression T>
using get_callable_t = typename get_callable<T>::type;
// 21.11.4.16 Function operations
template <Function T> struct get_pointer<T>;
// 21.11.4.17 RecordMemberFunction operations
template <RecordMemberFunction T> struct is_static<T>;
template <RecordMemberFunction T> struct is_const;
template <RecordMemberFunction T> struct is_volatile;
template <RecordMemberFunction T> struct has_lvalueref_qualifier;
template <RecordMemberFunction T> struct has_rvalueref_qualifier;
template <RecordMemberFunction T> struct is_virtual<T>;
template <RecordMemberFunction T> struct is_pure_virtual<T>;
template <RecordMemberFunction T> struct is_override;
template <RecordMemberFunction T> struct is_final<T>;
template <RecordMemberFunction T>
constexpr auto is_const_v = is_const<T>::value;
template <RecordMemberFunction T>
constexpr auto is_volatile_v = is_volatile<T>::value;
template <RecordMemberFunction T>
constexpr auto has_lvalueref_qualifier_v = has_lvalueref_qualifier<T>::value;
template <RecordMemberFunction T>
constexpr auto has_rvalueref_qualifier_v = has_rvalueref_qualifier<T>::value;
template <RecordMemberFunction T>
constexpr auto is_override_v = is_override<T>::value;
// 21.11.4.18 SpecialMemberFunction operations
template <SpecialMemberFunction T> struct is_implicitly_declared;
template <SpecialMemberFunction T> struct is_defaulted;
template <SpecialMemberFunction T>
constexpr auto is_implicitly_declared_v = is_implicitly_declared<T>::value;
template <SpecialMemberFunction T>
constexpr auto is_defaulted_v = is_defaulted<T>::value;
// 21.11.4.19 Constructor operations
template <Constructor T> struct is_explicit<T>;
// 21.11.4.20 Destructor operations
template <Destructor T> struct is_virtual<T>;
template <Destructor T> struct is_pure_virtual<T>;
// 21.11.4.21 ConversionOperator operations
template <ConversionOperator T> struct is_explicit<T>;
// 21.11.4.22 Lambda operations
template <Lambda T> struct get_captures;
template <Lambda T> struct uses_default_copy_capture;
template <Lambda T> struct uses_default_reference_capture;
template <Lambda T> struct is_call_operator_const;
template <Lambda T>
using get_captures_t = typename get_captures<T>::type;
template <Lambda T>
constexpr auto uses_default_copy_capture_v = uses_default_copy_capture<T>::value;
template <Lambda T>
constexpr auto uses_default_reference_capture_v = uses_default_reference_capture<T>::value;
template <Lambda T>
constexpr auto is_call_operator_const_v = is_call_operator_const<T>::value;
// 21.11.4.23 LambdaCapture operations
template <LambdaCapture T> struct is_explicitly_captured;
template <LambdaCapture T> struct is_init_capture;
template <LambdaCapture T>
constexpr auto is_explicitly_captured_v = is_explicitly_captured<T>::value;
template <LambdaCapture T>
constexpr auto is_init_capture_v = is_init_capture<T>::value;
Add the following paragraphs at the end of [reflect.concepts]:
FunctionParameter
[reflect.concepts.fctparam]
template <class T> concept FunctionParameter = see below;
FunctionParameter<T>
is satisfied if and only if T
reflects a function patameter. Any such T
also satisfies Named
and ScopeMember
. The Scope
of a FunctionParameter
is the Callable
this parameter appertains to.Callable
[reflect.concepts.callable]
template <class T> concept Callable = see below;
Callable<T>
is satisfied if and only if T
reflects a function including operators, constructors and destructors, or an object with a call operator, for instance a closure type. Any such T
also satisfies Named
, ScopeMember
and Scope
.FunctionCallExpression
[reflect.concepts.expr.fctcall]
template <class T> concept FunctionCallExpression = see below;
FunctionCallExpression<T>
is satisfied if and only if T
reflects a function call expression [expr.call]. Any such T
also satisfies Object
.Function
[reflect.concepts.fct]
template <class T> concept Function = see below;
Function<T>
is satisfied if and only if T
reflects an object with a call operator (for instance a closure object) or a function, excluding constructors and destructors. Any such T
also satisfies Callable
and Typed
.RecordMemberFunction
[reflect.concepts.memfct]
template <class T> concept RecordMemberFunction = see below;
RecordMemberFunction<T>
is satisfied if and only if T
reflects a member function, excluding constructors and destructors. Any such T
also satisfies RecordMember
and Function
.SpecialMemberFunction
[reflect.concepts.specialfct]
template <class T> concept SpecialMemberFunction = see below;
SpecialMemberFunction<T>
is satisfied if and only if T
reflects a special member function [special]. Any such T
also satisfies RecordMember
.Constructor
[reflect.concepts.ctor]
template <class T> concept Constructor = see below;
Constructor<T>
is satisfied if and only if T
reflects a constructor. Any such T
also satisfies Callable
and RecordMember
. Some specializations of Constructor
satisfy SpecialMemberFunction
.Destructor
[reflect.concepts.dtor]
template <class T> concept Destructor = see below;
Destructor<T>
is satisfied if and only if T
reflects a destructor. Any such T
also satisfies Callable
, SpecialMemberFunction
and RecordMember
.Operator
[reflect.concepts.oper]
template <class T> concept Operator = see below;
Operator<T>
is satisfied if and only if T
reflects an operator function [over.oper] or a conversion function [class.conv.fct]. Any such T
also satisfies Function
. Some specializations of Operator
satisfy RecordMemberFunction
or SpecialMemberFunction
.ConversionOperator
[reflect.concepts.convfct]
template <class T> concept ConversionOperator = see below;
ConversionOperator<T>
is satisfied if and only if T
reflects a conversion function [class.conv.fct]. Any such T
also satisfies Function
. Some specializations of Operator
satisfy RecordMemberFunction
.Lambda
[reflect.concepts.lambda]
template <class T> concept Lambda = see below;
Lambda<T>
is satisfied if and only if T
reflects an closure object (excluding generic lambdas). Any such T
also satisfies Function
.LambdaCapture
[reflect.concepts.lambdacapture]
template <class T> concept LambdaCapture = see below;
LambdaCapture<T>
is satisfied if and only if T
reflects a lambda capture as introduced by the capture list or by capture defaults. Any such T
also satisfies Variable
. The Scope
of a LambdaCapture
is its Lambda
.Modify [reflect.ops.over] as follows:
template <class T> struct is_public;
template <class T> struct is_protected;
template <class T> struct is_private;
template <class T> struct is_constexpr;
template <class T> struct is_static;
template <class T> struct is_final;
template <class T> struct get_pointer;
template <class T> struct is_explicit;
template <class T> struct is_inline;
template <class T> struct is_virtual;
template <class T> struct is_pure_virtual;
RecordMember
and Base
reflect
namespace.
Modify the relevant part of [reflect.ops.over] as follows:
T
reflecting a class data member, its unqualified nameT
reflecting a function, its unqualified name;T
reflecting a specialization of a template function, its template-name;T
reflecting a function parameter, its unqualified name;T
reflecting a constructor, the injected-class-name of its class;T
reflecting a destructor, the injected-class-name of its class, prefixed by the character '~';T
reflecting an operator function, the operator element of the relevant operator-function-id;T
reflecting an conversion function, the same characters as get_name_ve<R>
, with R
reflecting the type represented by the conversion-type-id.T
reflecting a lambda object), the string's value is the empty string for get_name<T> and implementation-defined for get_display_name<T>.Modify the relevant part of [reflect.ops.over] as follows:
T
, S is found as the innermost scope enclosing ST that is either a namespace scope (including global scope), class scope, Modify the relevant part of [reflect.ops.record] as follows:
template <Record T> struct get_public_data_members;
template <Record T> struct get_accessible_data_members;
template <Record T> struct get_data_members;
template <Record T> struct get_public_member_functions;
template <Record T> struct get_accessible_member_functions;
template <Record T> struct get_member_functions;
TransformationTrait
requirements ([meta.rqmts]). The nested type named type
is an alias to an ObjectSequence
specialized with RecordMember
types that reflect the following subset of T
:
get_data_members
(get_member_functions
), all data (function) members.get_public_data_members
(get_public_member_functions
), all public data (function) members;get_accessible_data_members
(get_accessible_member_functions
), all data (function) members that are accessible from the scope of the invocation of reflexpr
which (directly or indirectly) generated T
.ObjectSequence
is the order of the declaration of the T
.T
reflects a closure type.
template <Record T> struct get_constructors;
template <Record T> struct get_destructors;
template <Record T> struct get_operators;
TransformationTrait
requirements ([meta.rqmts]). The nested type named type
is an alias to an ObjectSequence
specialized with RecordMember
types that reflect the following subset of function members of the class reflected by T
:
get_constructors
, all constructors.get_destructors
, all destructors;get_operators
, all conversion functions [class.conv.fct] and operator functions [over.oper].ObjectSequence
is the order of the declaration of the members in the class reflected by T
.T
reflects a closure type.
Modify the relevant part of [reflect.ops.record] as follows:
template <Class T> struct is_final<T>;
is_final<T>
shall meet the UnaryTypeTrait
requirements ([meta.rqmts]). If T
reflects a class that is marked with the class-virt-specifier final
, the base characteristic of the respective template specialization is true_type
, otherwise it is false_type
.Modify the relevant part of [reflect.ops.value] as follows:
template <Variable T> struct is_constexpr<T>;
is_constexpr<T>
shall meet the UnaryTypeTrait
requirements ([meta.rqmts]). If T
reflects a variable declared with the decl-specifier constexpr
, the base characteristic of the respective template specialization is true_type
, otherwise it is false_type
.
template <Variable T> struct is_static<T>;
is_static<T>
shall meet the UnaryTypeTrait
requirements ([meta.rqmts]). If T
reflects a variable with static storage duration, the base characteristic of the respective template specialization is true_type
, otherwise it is false_type
.
template <Variable T> struct get_pointer<T>;
get_pointer<T>
shall meet the UnaryTypeTrait
requirements ([meta.rqmts]), with a static data member named value
of type X
and value x
, where
X
is add_pointer<Y>
, where Y
is the type of the variable reflected by T
and x
is the address of that variable; otherwise, X
is the pointer-to-member type of the member variable reflected by T
and x
a pointer to the member.Modify the relevant part of [reflect.ops.derived] as follows:
template <Base T> struct get_class;
get_class<T>
shall meet the TransformationTrait
requirements ([meta.rqmts]). The nested type named type
is an alias to reflexpr(X)
, where X
is the base class reflected by T
.
template <Base T> struct is_virtual<T>;
template <Base T> struct is_public<T>;
template <Base T> struct is_protected<T>;
template <Base T> struct is_private<T>;
Modify the relevant part of [reflect.ops.derived] as follows:
template <Namespace T> struct is_inline<T>;
is_inline<T>
shall meet the UnaryTypeTrait
requirements ([meta.rqmts]). If T
reflects an inline namespace, the base characteristic of the template specialization is true_type
, otherwise it is false_type
.Add the following paragraphs at the end of [reflect.ops]:
template <FunctionParameter T> struct is_ellipsis;
UnaryTypeTrait
requirements ([meta.rqmts]). If
T
reflects a terminating ellipsis of a function's
parameter-declaration-clause [dcl.fct], the base characteristic of
is_ellipsis<T>
is
true_type
, otherwise it is
false_type
.
template <FunctionParameter T> struct has_default_value;
UnaryTypeTrait
requirements ([meta.rqmts]). If
T
reflects a parameter with a default argument, the base characteristic of
is_enum<T>
(
is_union<T>
) is
true_type
, otherwise it is
false_type
.
template <Callable T> struct get_parameters;
TransformationTrait
requirements ([meta.rqmts]). The nested type named type
is an alias to an ObjectSequence
specialized with FunctionParameter
types that reflect the parameters of the function reflected by T
.
If that function's parameter-declaration-clause [dcl.fct] terminates with an ellipsis, the ObjectSequence
contains an additional, final element of type ELL
for which is_ellipsis_v<ELL>
is true
.
template <Callable T> struct is_constexpr<T>;
template <Callable T> struct is_noexcept<T>;
template <Callable T> struct is_inline<T>;
template <Callable T> struct is_deleted;
UnaryTypeTrait
requirements ([meta.rqmts]). If their template parameter reflects an entity that is (where applicable implicitly or explicitly) declared as constexpr
(for is_constexpr
), noexcept
(for is_noexcept
), as an inline function [dcl.inline] (for is_inline
), or as deleted (for is_deleted
), the base characteristic of the respective template specialization is true_type
, otherwise it is false_type
.
template <FunctionCallExpression T> struct get_callable;
get_callable<T>
shall meet the TransformationTrait
requirements ([meta.rqmts]). The nested type named type
is the Callable
type reflecting the function or callable object invoked by the function call expression which is reflected by T
.
template <Function T> struct get_pointer<T>;
get_pointer<T>
shall meet the UnaryTypeTrait
requirements ([meta.rqmts]), with a static data member named value
of type X
and value x
, where
X
is the pointer to member function type of the member function reflected by T
and x
a pointer to the member function; otheriwse,X
is add_pointer<Y>
, where Y
is the type of the function reflected by T
and x
is the address of that function.
template <RecordMemberFunction T> struct is_static<T>;
template <RecordMemberFunction T> struct is_const;
template <RecordMemberFunction T> struct is_volatile;
template <RecordMemberFunction T> struct has_lvalueref_qualifier;
template <RecordMemberFunction T> struct has_rvalueref_qualifier;
template <RecordMemberFunction T> struct is_virtual<T>;
template <RecordMemberFunction T> struct is_pure_virtual<T>;
template <RecordMemberFunction T> struct is_override;
template <RecordMemberFunction T> struct is_final<T>;
UnaryTypeTrait
requirements ([meta.rqmts]). If their template parameter reflects a member function that is static
(for is_static
), const
(for is_const
), volatile
(for is_volatile
), declared with a ref-qualifier &
(for has_lvalueref_qualifier
) or &&
(for has_rvalueref_qualifier
), implicitly or expicitly virtual
(for is_virtual
), pure virtual (for is_pure_virtual
), or marked with override
(for is_override
) or final
(for is_final
), the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <SpecialMemberFunction T> struct is_implicitly_declared;
template <SpecialMemberFunction T> struct is_defaulted;
UnaryTypeTrait
requirements ([meta.rqmts]). If their template parameter reflects a special member function that is implicitly declared (for is_implicitly_declared
) or that is defaulted (for is_defaulted
, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <Constructor T> struct is_explicit<T>;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects an explicit constructor, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <Destructor T> struct is_virtual<T>;
template <Destructor T> struct is_pure_virtual<T>;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects a virtual (for is_virtual
) or pure virtual (for is_pure_virtual
) destructor, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <ConversionOperator T> struct is_explicit<T>;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects an explicit conversion function, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <Lambda T> struct get_captures;
TransformationTrait
requirements ([meta.rqmts]). The nested type named type
is an alias to an ObjectSequence
specialized with LambdaCapture
types that reflect the captures of the closure object reflected by T
.
The elements are in order of appearance in the lambda-capture.
template <Lambda T> struct uses_default_copy_capture;
template <Lambda T> struct uses_default_reference_capture;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects a closure object with a capture-default that is =
(for uses_default_copy_capture
) or &
(for uses_default_reference_capture
), the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <Lambda T> struct is_call_operator_const;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects a closure object with a const
call operator, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <LambdaCapture T> struct is_explicitly_captured;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects an explicitly captured entity, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
template <LambdaCapture T> struct is_init_capture;
UnaryTypeTrait
requirements ([meta.rqmts]). If the template parameter reflects an init-capture, the base characteristic of the respective template specialization is
true_type
, otherwise it is
false_type
.
Following P0194's lead, function reflection requires a couple new concepts to restrict operations on the meta level. It builds upon the concepts introduced by P0194 and, like P0194, all declarations are inside the reflect
namespace.
Some of the concepts below extend P0194's concepts. Whenever a concept A requires another concept B, the operations defined for concept B are also available for concept A, building a graph similar to an inheritance graph.
Proper wording will be provided when needed (and when P0194 has progressed through LWG, increasing to the authors' experience on wording of reflection papers); we believe that missing wording should not hinder SG7's design discussion. The wording is planned to be similar to that of P0194; specifically, it uses nested type
and value
entities, with the usual using declarations ..._t
and ..._v
.
reflexpr
P0194 allows certain types and variables as operands of reflexpr
. This paper extends this set:
reflexpr
on a function call expression generates a FunctionCallExpression
;reflexpr
on a closure object generates a Lambda
;reflexpr
on a parameter name generates a FunctionParameter
;reflexpr
on a lambda capture generates a LambdaCapture
;
The way to obtain a Callable
might seem complicated. But instead of inventing a new syntax, for instance reflexpr(foo(int,int))
, re-using the mechanism of matching the address of an overloaded function name reflexpr((void(&)(int, int))foo)
, or syntax similar to function definition reflexpr(void foo(int,int))
, we rely on the existing, familiar rules for function overload resolution and generate the Callable
only when a concrete overload is selected. This approach also works for constructors and operators, for instance reflexpr(std::string())
reflects the call of the default constructor of string
; reflexpr(1+1)
reflects the call of the built-in addition operator for type int
.
FunctionParameter
template <class Object> concept FunctionParameter = /* implementation defined */;
Named
and ScopeMember
. Represents a function parameter. Unlike Variable
, it does not offer a get_pointer
interface. The name of a parameter is one of the names used in an unspecified declaration. If at least one of the declarations does not specify the parameter name, then get_name
is allowed to return an empty string. Its scope is the Callable
declaring this FunctionParameter
.
double Gauss(double x, double mean, double width, double height);
void f() {
// Don't confuse those!
func(true /*willLeave*/,
false /*knewWhatTheyVotedOn*/,
false /*willBeHappyEverAfter*/);
}
The bare combination of type and index is almost meaningless in these cases. There are many reflection applications that can benefit from this, for instance:
tuple
is not a replacement for classes.
template <typename T>
requires FunctionParameter<T>
struct is_ellipsis;
FunctionParameter
reflects an ellipsis.
template <typename T>
requires FunctionParameter<T>
struct has_default_value;
FunctionParameter
has a default value.Callable
template <class Object> concept Callable = /* implementation defined */;
Named
, ScopeMember
and Scope
. Represents a function or lambda, including operators, constructors and destructors - i.e. anything this paper is dealing with.
template <typename T>
requires Callable<T>
struct get_parameters;
ObjectSequence
of FunctionParameter
s of the reflected Callable
.
template <typename T>
requires Callable<T>
struct is_constexpr;
template <typename T>
requires Callable<T>
struct is_noexcept;
constexpr
or noexcept
, respectively.
template <typename T>
requires Callable<T>
struct is_inline;
struct X{ inline void f(); void g() {} }
, is_inline
is true
for both f
and g
.
template <typename T>
requires Callable<T>
struct is_deleted;
= delete
before the invocation of reflexpr
.FunctionCallExpression
template <class Object> concept FunctionCallExpression = /* implementation defined */;
Object
. Reflects a call of a concrete function or other callable.
template <typename T>
requires FunctionCallExpression<T>
struct get_callable;
Callable
that was invoked in a FunctionCallExpression
.Function
template <class Object> concept Function = /* implementation defined */;
Callable
and Typed
. Represents a function or lambda, excluding constructors and destructors.
template <typename T>
requires Function<T>
struct get_pointer;
auto p_sin = get_pointer_v<get_callable_t<reflexpr(sin(1.0))>>
holds the address of sin(double)
.RecordMemberFunction
template <class Object> concept RecordMemberFunction = /* implementation defined */;
RecordMember
and Function
. Represents a member function, excluding constructors and destructors.
template <typename T>
requires RecordMemberFunction<T>
struct is_static;
template <typename T>
requires RecordMemberFunction<T>
struct is_const;
template <typename T>
requires RecordMemberFunction<T>
struct is_volatile;
const
or volatile
, respectively.
template <typename T>
requires RecordMemberFunction<T>
struct has_lvalueref_qualifier;
template <typename T>
requires RecordMemberFunction<T>
struct has_rvalueref_qualifier;
&
or &&
, respectively.
template <typename T>
requires RecordMemberFunction<T>
struct is_virtual;
template <typename T>
requires RecordMemberFunction<T>
struct is_pure_virtual;
struct A { virtual void X(); };
struct B: A { void X(); };
the value of is_virtual_v<get_callable_t<reflexpr(std::declval<B>().X())>>
is true
, irrespectively of whether virtual
is implicit of explicit.
template <typename T>
requires RecordMemberFunction<T>
struct is_override;
template <typename T>
requires RecordMemberFunction<T>
struct is_final;
override
or final
, respectively.SpecialMemberFunction
template <class Object> concept SpecialMemberFunction = /* implementation defined */;
RecordMember
. Represents a special member function.
template <typename T>
requires SpecialMemberFunction<T>
struct is_implicitly_declared;
reflexpr
.
template <typename T>
requires SpecialMemberFunction<T>
struct is_defaulted;
= default
before the invocation of reflexpr
, independently of whether the special member function is implicitly or explicitly declared.Constructor
template <class Object> concept Constructor = /* implementation defined */;
Callable
and RecordMember
. Represents a constructor. The base name of the constructor is the base name of the constructor's class. Even though the standard explicitly says that constructors do not have a name, for usability purposes (e.g. generating messages), having them state the class name is a usability improvement.Constructor
might also satisfy SpecialMemberFunction
.
template <typename T>
requires Constructor<T>
struct is_explicit;
explicit
.Destructor
template <class Object> concept Destructor = /* implementation defined */;
Callable
, SpecialMemberFunction
and RecordMember
. Represents a destructor. The base name is the base name if the destructor's class, prefixed with '~'
.
template <typename T>
requires Destructor<T>
struct is_virtual;
template <typename T>
requires Destructor<T>
struct is_pure_virtual;
struct A { virtual ~A(); };
struct B: A { B(); };
the value of is_virtual_v<get_callable_t<reflexpr(std::declval<B>.~B())>>
is true
, irrespectively of whether virtual
is implicit of explicit.Operator
template <class Object> concept Operator = /* implementation defined */;
Function
. Some instances might implement RecordMemberFunction
or SpecialMemberFunction
. Represents an operator. The base name is the operator "symbol", for instance "+"
.ConversionOperator
template <class Object> concept ConversionOperator = /* implementation defined */;
Operator
. Represents a conversion operator. The base name is the base name of the operator's target type, for instance "int"
.
template <typename T>
requires ConversionOperator<T>
struct is_explicit;
explicit
.Lambda
template <class Object> concept Lambda = /* implementation defined */;
Function
. Represents a closure type, excluding those for generic lambdas. Its base name is the empty string.
template <typename T>
requires Lambda<T>
struct get_captures;
ObjectSequence
of LambdaCapture
s.
template <typename T>
requires Lambda<T>
struct uses_default_copy_capture;
=
.
template <typename T>
requires Lambda<T>
struct uses_default_reference_capture;
&
.
template <typename T>
requires Lambda<T>
struct is_call_operator_const;
false
if the lambda was declared as mutable
, true otherwise
.LambdaCapture
template <class Object> concept LambdaCapture = /* implementation defined */;
Variable
. Represents a lambda capture as introduced by the capture list or by capture defaults. The LambdaCapture
's scope is its Lambda
.
template <typename T>
requires LambdaCapture<T>
struct is_explicitly_captured;
template <typename T>
requires LambdaCapture<T>
struct is_init_capture;
Record
This proposal adds the following interfaces to the Record
concept of P0194:
template <typename T>
requires Record<T>
struct get_public_member_functions;
template <typename T>
requires Record<T>
struct get_accessible_member_functions;
template <typename T>
requires Record<T>
struct get_member_functions;
ObjectSequence
of RecordMemberFunctions
s representing a class's public member functions (for get_public_member_functions
), member functions accessible from the point of invocation of reflexpr
(for get_accessible_member_functions
) and all member functions, irrespective of their accessibility (for get_member_functions
). This includes static member functions, but not friend functions.
template <typename T>
requires Record<T>
struct get_constructors;
template <typename T>
requires Record<T>
struct get_destructors;
template <typename T>
requires Record<T>
struct get_operators;
ObjectSequence
of a class's Constructor
s, Destructor
s and Operator
s, respectively.Thanks to Jackie Kay who provided valuable feedback, criticism and suggestions!
1. Static reflection. Rationale, design and evolution. p0385
2. Static reflection in a nutshell. p0578