Advertisement
After introducing reflection in C++26 in my previous post, this time I am talking about metafunctions. These are called consteval
declared. consteval
Creates a so-called immediate function. Each call to an instantiated function creates a compile-time constant. To put it more clearly: a consteval
(Immediate) Function executes at compile time.
Rainer Grimm has been working as a software architect, team and training manager for many years. He enjoys writing articles on the programming languages C++, Python, and Haskell, but also frequently speaks at expert conferences. On his blog Modern C++ he discusses his passion C++ in depth.
more about consteval
And constinit
is in my article Two new keywords in C++20: consteval and constinit,
As we know so far:
constexpr auto r = ^int;
typename(:r:) x = 42; // Same as: int x = 42;
typename(:^char:) c = '*'; // Same as: char c = '*';
reflection operator (^
) Creates a reflection value from a grammatical element (C++ element). The data type of the reflection value is std::meta::info
The reflection value can be a metafunction or Spicer’s argument. ((: refl :)
) Happen. Spicers create grammatical elements.
Therefore the reflectance value can be defined as follows:
namespace std {
namespace meta {
using info = decltype(^::);
}
}
What is still missing? Of course meta functions are missing. I will introduce them all for two reasons:
- There are many meta functions.
- (As of October 2024) There is no official list of all meta functions.
metafunction
namespace std::meta {
using info = decltype(^::);
template
concept reflection_range = /* see above */;
// name and location
consteval auto identifier_of(info r) -> string_view;
consteval auto u8identifier_of(info r) -> u8string_view;
consteval auto display_string_of(info r) -> string_view;
consteval auto u8display_string_of(info r) -> u8string_view;
consteval auto source_location_of(info r) -> source_location;
// type queries
consteval auto type_of(info r) -> info;
consteval auto parent_of(info r) -> info;
consteval auto dealias(info r) -> info;
// object and value queries
consteval auto object_of(info r) -> info;
consteval auto value_of(info r) -> info;
// template queries
consteval auto template_of(info r) -> info;
consteval auto template_arguments_of(info r) -> vector;
// member queries
consteval auto members_of(info type_class) -> vector;
consteval auto bases_of(info type_class) -> vector;
consteval auto static_data_members_of(info type_class) -> vector;
consteval auto nonstatic_data_members_of(info type_class) -> vector;
consteval auto subobjects_of(info type_class) -> vector;
consteval auto enumerators_of(info type_enum) -> vector;
// member access
struct access_context {
static consteval access_context current() noexcept;
consteval access_context() noexcept;
};
consteval auto is_accessible(
info r,
acess_context from = access_context::current());
consteval auto accessible_members_of(
info target,
access_context from = access_context::current()) -> vector;
consteval auto accessible_bases_of(info target,
info target,
access_context from = access_context::current()) -> vector;
consteval auto accessible_nonstatic_data_members_of(
info target,
access_context from = access_context::current()) -> vector;
consteval auto accessible_static_data_members_of(
info target,
access_context from = access_context::current()) -> vector;
consteval auto accessible_subobjects_of(
info target,
access_context from = access_context::current()) -> vector;
// substitute
template >
consteval auto can_substitute(info templ, R&& args) -> bool;
template >
consteval auto substitute(info templ, R&& args) -> info;
// reflect_invoke
template >
consteval auto reflect_invoke(info target, R&& args) -> info;
template , reflection_range R2 = initializer_list>
consteval auto reflect_invoke(info target, R1&& tmpl_args, R2&& args) -> info;
// reflect expression results
template
consteval auto reflect_value(T value) -> info;
template
consteval auto reflect_object(T& value) -> info;
template
consteval auto reflect_function(T& value) -> info;
// extract
template
consteval auto extract(info) -> T;
// other type predicates (see the wording)
consteval auto is_public(info r) -> bool;
consteval auto is_protected(info r) -> bool;
consteval auto is_private(info r) -> bool;
consteval auto is_virtual(info r) -> bool;
consteval auto is_pure_virtual(info entity) -> bool;
consteval auto is_override(info entity) -> bool;
consteval auto is_final(info r) -> bool;
consteval auto is_deleted(info entity) -> bool;
consteval auto is_defaulted(info entity) -> bool;
consteval auto is_explicit(info entity) -> bool;
consteval auto is_noexcept(info entity) -> bool;
consteval auto is_bit_field(info entity) -> bool;
consteval auto is_enumerator(info entity) -> bool;
consteval auto is_const(info r) -> bool;
consteval auto is_volatile(info r) -> bool;
consteval auto is_lvalue_reference_qualified(info r) -> bool;
consteval auto is_rvalue_reference_qualified(info r) -> bool;
consteval auto has_static_storage_duration(info r) -> bool;
consteval auto has_thread_storage_duration(info r) -> bool;
consteval auto has_automatic_storage_duration(info r) -> bool;
consteval auto has_internal_linkage(info r) -> bool;
consteval auto has_module_linkage(info r) -> bool;
consteval auto has_external_linkage(info r) -> bool;
consteval auto has_linkage(info r) -> bool;
consteval auto is_class_member(info entity) -> bool;
consteval auto is_namespace_member(info entity) -> bool;
consteval auto is_nonstatic_data_member(info entity) -> bool;
consteval auto is_static_member(info entity) -> bool;
consteval auto is_base(info entity) -> bool;
consteval auto is_data_member_spec(info r) -> bool;
consteval auto is_namespace(info entity) -> bool;
consteval auto is_function(info entity) -> bool;
consteval auto is_variable(info entity) -> bool;
consteval auto is_type(info entity) -> bool;
consteval auto is_type_alias(info entity) -> bool;
consteval auto is_namespace_alias(info entity) -> bool;
consteval auto is_complete_type(info entity) -> bool;
consteval auto is_template(info entity) -> bool;
consteval auto is_function_template(info entity) -> bool;
consteval auto is_variable_template(info entity) -> bool;
consteval auto is_class_template(info entity) -> bool;
consteval auto is_alias_template(info entity) -> bool;
consteval auto is_conversion_function_template(info entity) -> bool;
consteval auto is_operator_function_template(info entity) -> bool;
consteval auto is_literal_operator_template(info entity) -> bool;
consteval auto is_constructor_template(info entity) -> bool;
consteval auto is_concept(info entity) -> bool;
consteval auto is_structured_binding(info entity) -> bool;
consteval auto is_value(info entity) -> bool;
consteval auto is_object(info entity) -> bool;
consteval auto has_template_arguments(info r) -> bool;
consteval auto has_default_member_initializer(info r) -> bool;
consteval auto is_special_member(info r) -> bool;
consteval auto is_conversion_function(info r) -> bool;
consteval auto is_operator_function(info r) -> bool;
consteval auto is_literal_operator(info r) -> bool;
consteval auto is_constructor(info r) -> bool;
consteval auto is_default_constructor(info r) -> bool;
consteval auto is_copy_constructor(info r) -> bool;
consteval auto is_move_constructor(info r) -> bool;
consteval auto is_assignment(info r) -> bool;
consteval auto is_copy_assignment(info r) -> bool;
consteval auto is_move_assignment(info r) -> bool;
consteval auto is_destructor(info r) -> bool;
consteval auto is_user_provided(info r) -> bool;
// define_class
struct data_member_options_t;
consteval auto data_member_spec(info type_class,
data_member_options_t options = {}) -> info;
template >
consteval auto define_class(info type_class, R&&) -> info;
// define_static_string
consteval auto define_static_string(string_view str) -> const char *;
consteval auto define_static_string(u8string_view str) -> const char8_t *;
// data layout
struct member_offsets {
size_t bytes;
size_t bits;
constexpr auto total_bits() const -> . double quote. double quote. double quote. double quote. size_t;
auto operator<=>(member_offsets const&) const = default;
};
consteval auto offset_of(info entity) -> member_offsets;
consteval auto size_of(info entity) -> size_t;
consteval auto alignment_of(info entity) -> size_t;
consteval auto bit_size_of(info entity) -> size_t;
}
First, meta functions can remind you of type symptoms-Meta function. However, there is one big difference: type-traits are called meta-functions. constexpr
is declared, but declared as reflection meta functions consteval
This means that the type’s trait metafunction is executed at compile time. can doBut reflection meta functions are executed at compile time. SureAs a result, their arguments must be consistent expressions.
The Reflection library has metafunctions for the following functions:
- Returns name or location,
- Returns a reflection value of a data type, object and value, template and member,
- returns member access,
- Returns a reflection of the template arguments,
- Returns the result of the call,
- Returns the result of the evaluated expression and
- Extracts the same values as Splicers.
Additionally, the Reflection library contains over 50 compile-time predicates. These are callable elements that are created at compile time. bool
Return
Additionally there are meta functions to define a class.
What will happen next?
In my next article I will implement meta functions.
(rme)