C++ keywords: reflexpr (reflection TS)
From cppreference.com
Contents |
[edit] Usage
- gets the member list of a class type, or the enumerator list of an enum type.
- gets the name of type and member.
- detects whether a data member is static or constexpr.
- detects whether member function is virtual, public, protected or private.
- get the row and column of the source code when the type defines.
There're 2 papers of reflections. N4856, and P1240R1. N4856 gives <experimental/reflect> and P1240R1 gives <experimental/meta>.
[edit] Example
reflexpr
provides us the meta info of the object via meta-object types. Note that std::reflect::get_data_members_t
make programmers able to visit any class just like std::tuple.
Run this code
#include <string> #include <vector> struct S { int b; std::string s; std::vector<std::string> v; }; // Reflection TS #include <experimental/reflect> using meta_S = reflexpr(S); using mem = std::reflect::get_data_members_t<meta_S>; using meta = std::reflect::get_data_members_t<mem>; static_assert(std::reflect::is_public_v<meta>); // successful // P1240R1's meta library is easier #include <experimantal/meta> constexpr bool p = std::meta::is_public(reflexpr(S)); static_assert(p); // successful int main() {}
We can also know the name info from reflexpr:
Run this code
#include <string> #include <string_view> #include <iostream> // Reflection TS #include <experimental/reflect> template <typename Tp> constexpr std::string_view nameof() { using TpInfo = reflexpr(Tp); using aliased_Info = std::experimental::reflect::get_aliased_t<TpInfo>; return std::experimental::reflect::get_name_v<aliased_Info>; } // P1240R1's meta library is easier! #include <experimental/meta> template<typename T> constexpr std::string_view nameof() { return std::meta::name_of(reflexpr(T)); } int main(){ std::cout << nameof<std::string>() << '\n'; static_assert(nameof<std::string>() == "basic_string"); // successful }
This is an example of getting the scope of a type in the Reflection TS.
Run this code
namespace Foo{ struct FooFoo{int FooFooFoo}; } namespace Bar{ using BarBar = ::Foo::FooFoo; } using BarBarInfo = reflexpr(::Bar::BarBar); using BarBarScope = ::std::experimental::reflect::get_scope_t<BarBarInfo>; // Bar, not Foo struct Spam{int SpamSpam;}; struct Grok{ using GrokGrok = Spam::SpamSpam; }; using GrokGrokInfo = reflexpr(::Grok::GrokGrok); using GrokGrokScope = std::experimental::reflect::get_scope_t<GrokGrokInfo>; // Grok, not Spam
[edit] Scalable reflection
Facilities introduced by P1240 are described here.
[edit] Meta functions
Declared in header <experimental/meta>.
#include <span> #include <string> #include <vector> namespace std::experimental::meta { using info = decltype(reflexpr(void)); consteval bool is_invalid(info reflection); consteval bool is_local(info reflection); consteval bool is_class_member(info reflection); consteval bool is_variable(info reflection); consteval bool has_static_storage_duration(info variable); consteval bool has_thread_local_storage_duration(info variable); consteval bool has_automatic_storage_duration(info variable); consteval bool is_function(info reflection); consteval bool is_nothrow(info function); consteval bool has_ellipsis(info function); template<typename ...Args> consteval std::vector<info> members_of(info class_type, Args ...filters); template<typename ...Args> consteval std::vector<info> bases_of(info class_type, Args ...filters); consteval bool is_class(info reflection); consteval bool is_union(info reflection); consteval bool has_virtual_destructor(info class_type); consteval bool is_declared_class(info class_type); consteval bool is_declared_struct(info class_type); consteval bool is_data_member(info reflection); consteval bool is_static_data_member(info reflection); consteval bool is_nonstatic_data_member(info reflection); consteval bool is_bit_field(info reflection); consteval bool is_mutable(info reflection); consteval bool is_member_function(info reflection); consteval bool is_static_member_function(info reflection); consteval bool is_nonstatic_member_function(info reflection); consteval bool is_normal(info mem_function); consteval bool is_conversion(info mem_function); consteval bool is_override(info mem_function); consteval bool is_override_specified(info mem_function); consteval bool is_deleted(info mem_function); consteval bool is_virtual(info mem_function); consteval bool is_pure_virtual(info mem_function); consteval bool is_constructor(info reflection); consteval bool is_default_constructor(info reflection); consteval bool is_copy_constructor(info reflection); consteval bool is_move_constructor(info reflection); consteval bool is_copy_assignment_operator(info reflection); consteval bool is_move_assignment_operator(info reflection); consteval bool is_copy(info mem_function); consteval bool is_move(info mem_function); consteval bool is_destructor(info reflection); consteval bool is_defaulted(info spec_mem_function); consteval bool is_explicit(info spec_mem_function); consteval bool has_access(info reflection); consteval bool is_public(info base_or_mem); consteval bool is_protected(info base_or_mem); consteval bool is_private(info base_or_mem); consteval bool has_default_access(info base_or_mem); consteval bool has_linkage(info reflection); consteval bool is_externally_linked(info reflection); consteval bool is_internally_linked(info reflection); consteval bool is_extern_specified(info reflection); consteval bool is_inline(info reflection); consteval bool is_inline_specified(info reflection); consteval bool is_constexpr(info reflection); consteval bool is_consteval(info reflection); consteval bool is_final(info reflection); consteval bool is_defined(info reflection); consteval bool is_complete(info reflection); consteval bool is_namespace(info reflection); consteval bool is_alias(info reflection); consteval bool is_namespace_alias(info reflection); consteval bool is_type_alias(info reflection); consteval bool is_alias_template(info reflection); consteval bool is_enum(info reflection); consteval bool is_unscoped_enum(info reflection); consteval bool is_scoped_enum(info reflection); consteval std::vector<info> enumerators_of(info enum_type); consteval bool is_enumerator(info reflection); consteval bool is_template(info reflection); consteval bool is_class_template(info reflection); consteval bool is_function_template(info reflection); consteval bool is_variable_template(info reflection); consteval bool is_member_function_template(info reflection); consteval bool is_static_member_function_template(reflection); consteval bool is_nonstatic_member_function_template(info reflection); consteval bool is_constructor_template(info reflection); consteval bool is_destructor_template(info reflection); consteval bool is_concept(info reflection); consteval bool is_specialization(info reflection); consteval bool is_partial_specialization(info reflection); consteval bool is_explicit_specialization(info reflection); consteval bool is_implicit_instantiation(info reflection); consteval bool is_explicit_instantiation(info reflection); consteval info template_of(info special); consteval bool has_template_arguments(info reflection); consteval std::vector<info> template_arguments_of(info special); consteval info substitute(info templ, std::span<info> arguments); consteval bool is_base_class(info reflection); consteval bool is_direct_base_class(info reflection); consteval bool is_virtual_base_class(info reflection); consteval bool is_function_parameter(info reflection); consteval bool is_template_parameter(info reflection); consteval bool is_type_template_parameter(info reflection); consteval bool is_nontype_template_parameter(info reflection); consteval bool is_template_template_parameter(info reflection); consteval bool has_default_argument(info parameter); consteval std::vector<info> parameters_of(info function_or_templ); consteval bool is_type(info reflection); consteval bool is_fundamental_type(info type); consteval bool has_fundamental_type(info reflection); consteval bool is_arithmetic_type(info type); consteval bool has_arithmetic_type(info reflection); consteval bool is_scalar_type(info type); consteval bool has_scalar_type(info reflection); consteval bool is_object_type(info type); consteval bool has_object_type(info reflection); consteval bool is_compound_type(info type); consteval bool has_compound_type(info reflection); consteval bool is_function_type(info type); consteval bool has_function_type(info reflection); consteval bool is_class_type(info type); consteval bool has_class_type(info reflection); consteval bool is_union_type(info type); consteval bool has_union_type(info reflection); consteval bool is_enum_type(info type); consteval bool has_enum_type(info type); consteval bool is_unscoped_enum_type(info type); consteval bool has_unscoped_enum_type(info reflection); consteval bool is_scoped_enum_type(info type); consteval bool has_scoped_enum_type(info reflection); consteval bool is_void_type(info type); consteval bool has_void_type(info reflection); consteval bool is_null_pointer_type(info type); consteval bool has_null_pointer_type(info reflection); consteval bool is_integral_type(info type); consteval bool has_integral_type(info reflection); consteval bool is_floating_point_type(info type); consteval bool has_floating_point_type(info reflection); consteval bool is_array_type(info type); consteval bool has_array_type(info reflection); consteval bool is_pointer_type(info type); consteval bool has_pointer_type(info reflection); consteval bool is_reference_type(info type); consteval bool has_reference_type(info reflection); consteval bool is_lvalue_reference_type(info type); consteval bool has_lvalue_reference_type(info reflection); consteval bool is_rvalue_reference_type(info type); consteval bool has_rvalue_reference_type(info reflection); consteval bool is_member_pointer_type(info type); consteval bool has_member_pointer_type(info reflection); consteval bool is_member_object_pointer_type(info type); consteval bool has_member_object_pointer_type(info reflection); consteval bool is_member_function_pointer_type(info type); consteval bool has_member_function_pointer_type(info reflection); consteval bool is_closure_type(info type); consteval bool has_closure_type(info reflection); consteval bool is_incomplete_type(info type); consteval bool has_incomplete_type(info reflection); consteval bool is_const_type(info type); consteval bool has_const_type(info reflection); consteval bool is_volatile_type(info type); consteval bool has_volatile_type(info reflection); consteval bool is_trivial_type(info type); consteval bool has_trivial_type(info reflection); consteval bool is_trivially_copyable_type(info type); consteval bool has_trivially_copyable_type(info reflection); consteval bool is_standard_layout_type(info type); consteval bool has_standard_layout_type(info reflection); consteval bool is_pod_type(info type); consteval bool has_pod_type(info reflection); consteval bool is_literal_type(info type); consteval bool has_literal_type(info reflection); consteval bool is_empty_type(info type); consteval bool has_empty_type(info reflection); consteval bool is_polymorphic_type(info type); consteval bool has_polymorphic_type(info reflection); consteval bool is_abstract_type(info type); consteval bool has_abstract_type(info reflection); consteval bool is_final_type(info type); consteval bool has_final_type(info reflection); consteval bool is_aggregate_type(info type); consteval bool has_aggregate_type(info reflection); consteval bool is_signed_type(info type); consteval bool has_signed_type(info reflection); consteval bool is_unsigned_type(info type); consteval bool has_unsigned_type(info reflection); consteval bool has_unique_object_representations(info type); consteval bool has_type_with_unique_object_representations(info reflection); consteval std::size_t size_of(info reflection); consteval std::size_t byte_size_of(info reflection); consteval std::size_t bit_size_of(info reflection); consteval std::size_t byte_offset_of(info reflection); consteval std::size_t bit_offset_of(info reflection); consteval std::size_t alignment_of(info reflection); consteval std::size_t rank(info reflection); consteval std::size_t extent(info reflection); consteval bool is_constructible(info reflection, std::span<info> arguments); consteval bool is_trivially_constructible(info reflection, std:span<info> arguments); consteval bool is_nothrow_constructible(info reflection, std::span<info> arguments); consteval bool is_default_constructible_type(info type); consteval bool has_default_constructible_type(info reflection); consteval bool is_trivially_default_constructible_type(info type); consteval bool has_trivially_default_constructible_type(info reflection); consteval bool is_nothrow_default_constructible_type(info type); consteval bool has_nothrow_default_constructible_type(info reflection); consteval bool is_copy_constructible_type(info type); consteval bool has_copy_constructible_type(info reflection); consteval bool is_trivially_copy_constructible_type(info type); consteval bool has_trivially_copy_constructible_type(info reflection); consteval bool is_nothrow_copy_constructible_type(info type); consteval bool has_nothrow_copy_constructible_type(info reflection); consteval bool is_move_constructible_type(info type); consteval bool has_move_constructible_type(info reflection); consteval bool is_trivially_move_constructible_type(info type); consteval bool has_trivially_move_constructible_type(info reflection); consteval bool is_nothrow_move_constructible_type(info type); consteval bool has_nothrow_move_constructible_type(info reflection); consteval bool is_assignable_type(info type, info assigned_type); consteval bool is_trivially_assignable_type(info type, info assigned_type); consteval bool is_nothrow_assignable_type(info type, info assigned_type); consteval bool is_copy_assignable_type(info type); consteval bool has_copy_assignable_type(info reflection); consteval bool is_trivially_copy_assignable_type(info type); consteval bool has_trivially_copy_assignable_type(info reflection); consteval bool is_nothrow_copy_assignable_type(info type); consteval bool has_nothrow_copy_assignable_type(info reflection); consteval bool is_move_assignable_type(info type); consteval bool has_move_assignable_type(info reflection); consteval bool is_trivially_move_assignable_type(info type); consteval bool has_trivially_move_assignable_type(info reflection); consteval bool is_nothrow_move_assignable_type(info type); consteval bool has_nothrow_move_assignable_type(info reflection); consteval bool is_destructible_type(info type); consteval bool has_destructible_type(info reflection); consteval bool is_trivially_destructible_type(info type); consteval bool has_trivially_destructible_type(info reflection); consteval bool is_nothrow_destructible_type(info type); consteval bool has_nothrow_destructible_type(info reflection); consteval bool is_swappable(info reflection); consteval bool is_nothrow_swappable(info reflection); consteval bool is_swappable_with(info reflection1, info reflection2); consteval bool is_nothrow_swappable_with(info reflection1, info reflection2); consteval std::vector<info> captures_of(info reflection); consteval bool has_default_ref_capture(info reflection); consteval bool has_default_copy_capture(info reflection); consteval bool is_capture(info reflection); consteval bool is_simple_capture(info reflection); consteval bool is_ref_capture(info reflection); consteval bool is_copy_capture(info reflection); consteval bool is_explicit_capture(info reflection); consteval bool is_init_capture(info reflection); consteval bool has_captures(info reflection); consteval bool is_same(info reflection1, info reflection2); consteval bool is_base_of(info base_type, info derived_type); consteval bool is_convertible(info from_type, info to_type); consteval bool is_nothrow_convertible(info from_type, info to_type); consteval bool is_invocable(info function, std::span<info> arguments); consteval bool is_nothrow_invocable(info function, std::span<info> arguments); consteval bool is_invocable_r(info function, std::span<info> arguments, info result); consteval bool is_nothrow_invocable_r(info function, std::span<info> arguments, info result); consteval info remove_const(info type); consteval info remove_volatile(info type); consteval info remove_cv(info type); consteval info add_const(info type); consteval info add_volatile(info type); consteval info add_cv(info type); consteval info remove_reference(info type); consteval info add_lvalue_reference(info type); consteval info add_rvalue_reference(info type); consteval info remove_pointer(info type); consteval info add_pointer(info type); consteval info remove_cvref(info type); consteval info decay(info type); consteval info make_signed(info type); consteval info make_unsigned(info type); consteval info aligned_storage(std::size_t length, std::size_t align = /* __DefaultAlignment */); consteval info aligned_union(std::size_t length, std::span<info> types); consteval info enable_if(bool cond, info type = reflexpr(void)); consteval info this_ref_type_of(info mem_function); consteval info common_type(const std::vector<info>& types); consteval info underlying_type_of(info reflection); consteval info invoke_result(info function, std::span<info> arguments); consteval info type_of(info reflection); consteval info return_type_of(info function); consteval bool is_entity(info reflection); consteval info entity_of(info reflection); consteval info parent_of(info reflection); consteval info definition_of(info reflection); consteval bool is_named(info reflection); consteval std::string name_of(info named); consteval std::string display_name_of(info named); consteval bool is_lvalue(info reflection); consteval bool is_xvalue(info reflection); consteval bool is_prvalue(info reflection); consteval bool is_glvalue(info reflection); consteval bool is_rvalue(info reflection); }
This section is incomplete |