标准库标头 <meta> (C++26)

来自cppreference.com


 
 
标准库头
 

包含

(C++20)
三路比较运算符支持[编辑]
std::initializer_list 类模板[编辑]

概念

在命名空间 std::meta 定义
指定反射值组成的范围
(概念) [编辑]

类型

在命名空间 std::meta 定义
(C++26)
反射值的类型
(typedef) [编辑]
反射函数抛出的异常
(类) [编辑]
(C++26)
标明可重载运算符的常量
(枚举) [编辑]
访问检查的上下文
(类) [编辑]
成员或基类相对于其外围类的偏移量
(类) [编辑]
包括名称、对齐和位宽在内的数据成员选项
(类) [编辑]

函数

在命名空间 std 定义
将编译期字符串提升为静态存储,返回指向该静态字符串首字符的指针
(函数模板) [编辑]
将编译期数组提升为静态存储,返回静态数组的跨度。
(函数模板) [编辑]
将编译期值提升为静态存储,返回指向该静态对象的指针。
(函数模板) [编辑]
在命名空间 std::meta 定义
将编译期字符串提升为静态存储,返回表示该静态字符串的反射。
(函数模板) [编辑]
将编译期数组提升为静态存储,返回表示该静态数组的反射
(函数模板) [编辑]

概要

#include <compare>
#include <initializer_list>

namespace std {
  // 检查字符串字面量
  consteval bool is_string_literal(const char* p);
  consteval bool is_string_literal(const wchar_t* p);
  consteval bool is_string_literal(const char8_t* p);
  consteval bool is_string_literal(const char16_t* p);
  consteval bool is_string_literal(const char32_t* p);

  // 提升为静态存储
  namespace meta {
    template<ranges::input_range R> consteval info reflect_constant_string(R&& r);
    template<ranges::input_range R> consteval info reflect_constant_array(R&& r);
  }
  template<ranges::input_range R>
  consteval const ranges::range_value_t<R>* define_static_string(R&& r);
  template<ranges::input_range R>
  consteval span<const ranges::range_value_t<R>, /* 见描述 */>
  define_static_array(R&& r);
  template<class T> consteval const remove_cvref_t<T>* define_static_object(T&& r);
}

namespace std::meta {
  using info = decltype(^^::);

  // 类 exception
  class exception;

  // 运算符表示
  enum class operators
  {
    /* 见描述 */;
  };
  using enum operators;
  consteval operators operator_of(info r);
  consteval string_view symbol_of(operators op);
  consteval u8string_view u8symbol_of(operators op);

  // 反射名字和源码位置
  consteval bool has_identifier(info r);

  consteval string_view identifier_of(info r);
  consteval u8string_view u8identifier_of(info r);

  consteval string_view display_string_of(info r);
  consteval u8string_view u8display_string_of(info r);

  consteval source_location source_location_of(info r);

  // 反射查询
  consteval info type_of(info r);
  consteval info object_of(info r);
  consteval info constant_of(info r);

  consteval bool is_public(info r);
  consteval bool is_protected(info r);
  consteval bool is_private(info r);

  consteval bool is_virtual(info r);
  consteval bool is_pure_virtual(info r);
  consteval bool is_override(info r);
  consteval bool is_final(info r);

  consteval bool is_deleted(info r);
  consteval bool is_defaulted(info r);
  consteval bool is_user_provided(info r);
  consteval bool is_user_declared(info r);
  consteval bool is_explicit(info r);
  consteval bool is_noexcept(info r);

  consteval bool is_bit_field(info r);
  consteval bool is_enumerator(info r);
  consteval bool is_annotation(info r);

  consteval bool is_const(info r);
  consteval bool is_volatile(info r);
  consteval bool is_mutable_member(info r);
  consteval bool is_lvalue_reference_qualified(info r);
  consteval bool is_rvalue_reference_qualified(info r);

  consteval bool has_static_storage_duration(info r);
  consteval bool has_thread_storage_duration(info r);
  consteval bool has_automatic_storage_duration(info r);

  consteval bool has_internal_linkage(info r);
  consteval bool has_module_linkage(info r);
  consteval bool has_external_linkage(info r);
  consteval bool has_c_language_linkage(info r);
  consteval bool has_linkage(info r);

  consteval bool is_complete_type(info r);
  consteval bool is_enumerable_type(info r);

  consteval bool is_variable(info r);
  consteval bool is_type(info r);
  consteval bool is_namespace(info r);
  consteval bool is_type_alias(info r);
  consteval bool is_namespace_alias(info r);

  consteval bool is_function(info r);
  consteval bool is_conversion_function(info r);
  consteval bool is_operator_function(info r);
  consteval bool is_literal_operator(info r);
  consteval bool is_special_member_function(info r);
  consteval bool is_constructor(info r);
  consteval bool is_default_constructor(info r);
  consteval bool is_copy_constructor(info r);
  consteval bool is_move_constructor(info r);
  consteval bool is_assignment(info r);
  consteval bool is_copy_assignment(info r);
  consteval bool is_move_assignment(info r);
  consteval bool is_destructor(info r);

  consteval bool is_function_parameter(info r);
  consteval bool is_explicit_object_parameter(info r);
  consteval bool has_default_argument(info r);
  consteval bool is_vararg_function(info r);

  consteval bool is_template(info r);
  consteval bool is_function_template(info r);
  consteval bool is_variable_template(info r);
  consteval bool is_class_template(info r);
  consteval bool is_alias_template(info r);
  consteval bool is_conversion_function_template(info r);
  consteval bool is_operator_function_template(info r);
  consteval bool is_literal_operator_template(info r);
  consteval bool is_constructor_template(info r);
  consteval bool is_concept(info r);

  consteval bool is_value(info r);
  consteval bool is_object(info r);

  consteval bool is_structured_binding(info r);

  consteval bool is_class_member(info r);
  consteval bool is_namespace_member(info r);
  consteval bool is_nonstatic_data_member(info r);
  consteval bool is_static_member(info r);
  consteval bool is_base(info r);

  consteval bool has_default_member_initializer(info r);

  consteval bool has_parent(info r);
  consteval info parent_of(info r);

  consteval info dealias(info r);

  consteval bool has_template_arguments(info r);
  consteval info template_of(info r);
  consteval vector<info> template_arguments_of(info r);
  consteval vector<info> parameters_of(info r);
  consteval info variable_of(info r);
  consteval info return_type_of(info r);

  // 访问控制上下文
  struct access_context;

  // 成员可访问性查询
  consteval bool is_accessible(info r, access_context ctx);
  consteval bool has_inaccessible_nonstatic_data_members(info r, access_context ctx);
  consteval bool has_inaccessible_bases(info r, access_context ctx);
  consteval bool has_inaccessible_subobjects(info r, access_context ctx);

  // 作用域识别
  consteval info current_function();
  consteval info current_class();
  consteval info current_namespace();

  // 反射成员查询
  consteval vector<info> members_of(info r, access_context ctx);
  consteval vector<info> bases_of(info type, access_context ctx);
  consteval vector<info> static_data_members_of(info type, access_context ctx);
  consteval vector<info> nonstatic_data_members_of(info type, access_context ctx);
  consteval vector<info> subobjects_of(info type, access_context ctx);
  consteval vector<info> enumerators_of(info type_enum);

  // 反射布局查询
  struct member_offset;
  consteval member_offset offset_of(info r);
  consteval size_t size_of(info r);
  consteval size_t alignment_of(info r);
  consteval size_t bit_size_of(info r);

  // 标注反射
  consteval vector<info> annotations_of(info item);
  consteval vector<info> annotations_of_with_type(info item, info type);

  // 值提取
  template<class T> consteval T extract(info r);

  // 反射代换
  template<class R>
  concept reflection_range = /* 见描述 */;

  template<reflection_range R = initializer_list<info>>
  consteval bool can_substitute(info templ, R&& arguments);
  template<reflection_range R = initializer_list<info>>
  consteval info substitute(info templ, R&& arguments);

  // 表达式结果反射
  template<class T> consteval info reflect_constant(T expr);
  template<class T> consteval info reflect_object(T& expr);
  template<class T> consteval info reflect_function(T& fn);

  // 类定义生成
  struct data_member_options;
  consteval info data_member_spec(info type, data_member_options options);
  consteval bool is_data_member_spec(info r);
  template<reflection_range R = initializer_list<info>>
  consteval info define_aggregate(info type_class, R&& mdescrs);

  // 基础类型分类
  consteval bool is_void_type(info type);
  consteval bool is_null_pointer_type(info type);
  consteval bool is_integral_type(info type);
  consteval bool is_floating_point_type(info type);
  consteval bool is_array_type(info type);
  consteval bool is_pointer_type(info type);
  consteval bool is_lvalue_reference_type(info type);
  consteval bool is_rvalue_reference_type(info type);
  consteval bool is_member_object_pointer_type(info type);
  consteval bool is_member_function_pointer_type(info type);
  consteval bool is_enum_type(info type);
  consteval bool is_union_type(info type);
  consteval bool is_class_type(info type);
  consteval bool is_function_type(info type);
  consteval bool is_reflection_type(info type);

  // 复合类型分类
  consteval bool is_reference_type(info type);
  consteval bool is_arithmetic_type(info type);
  consteval bool is_fundamental_type(info type);
  consteval bool is_object_type(info type);
  consteval bool is_scalar_type(info type);
  consteval bool is_compound_type(info type);
  consteval bool is_member_pointer_type(info type);

  // 类型的性质
  consteval bool is_const_type(info type);
  consteval bool is_volatile_type(info type);
  consteval bool is_trivially_copyable_type(info type);
  consteval bool is_standard_layout_type(info type);
  consteval bool is_empty_type(info type);
  consteval bool is_polymorphic_type(info type);
  consteval bool is_abstract_type(info type);
  consteval bool is_final_type(info type);
  consteval bool is_aggregate_type(info type);
  consteval bool is_structural_type(info type);
  consteval bool is_signed_type(info type);
  consteval bool is_unsigned_type(info type);
  consteval bool is_bounded_array_type(info type);
  consteval bool is_unbounded_array_type(info type);
  consteval bool is_scoped_enum_type(info type);

  template<reflection_range R = initializer_list<info>>
  consteval bool is_constructible_type(info type, R&& type_args);
  consteval bool is_default_constructible_type(info type);
  consteval bool is_copy_constructible_type(info type);
  consteval bool is_move_constructible_type(info type);

  consteval bool is_assignable_type(info type_dst, info type_src);
  consteval bool is_copy_assignable_type(info type);
  consteval bool is_move_assignable_type(info type);

  consteval bool is_swappable_with_type(info type1, info type2);
  consteval bool is_swappable_type(info type);

  consteval bool is_destructible_type(info type);

  template<reflection_range R = initializer_list<info>>
  consteval bool is_trivially_constructible_type(info type, R&& type_args);
  consteval bool is_trivially_default_constructible_type(info type);
  consteval bool is_trivially_copy_constructible_type(info type);
  consteval bool is_trivially_move_constructible_type(info type);

  consteval bool is_trivially_assignable_type(info type_dst, info type_src);
  consteval bool is_trivially_copy_assignable_type(info type);
  consteval bool is_trivially_move_assignable_type(info type);
  consteval bool is_trivially_destructible_type(info type);

  template<reflection_range R = initializer_list<info>>
  consteval bool is_nothrow_constructible_type(info type, R&& type_args);
  consteval bool is_nothrow_default_constructible_type(info type);
  consteval bool is_nothrow_copy_constructible_type(info type);
  consteval bool is_nothrow_move_constructible_type(info type);

  consteval bool is_nothrow_assignable_type(info type_dst, info type_src);
  consteval bool is_nothrow_copy_assignable_type(info type);
  consteval bool is_nothrow_move_assignable_type(info type);

  consteval bool is_nothrow_swappable_with_type(info type1, info type2);
  consteval bool is_nothrow_swappable_type(info type);

  consteval bool is_nothrow_destructible_type(info type);

  consteval bool is_implicit_lifetime_type(info type);

  consteval bool has_virtual_destructor(info type);

  consteval bool has_unique_object_representations(info type);

  consteval bool reference_constructs_from_temporary(info type_dst, info type_src);
  consteval bool reference_converts_from_temporary(info type_dst, info type_src);

  // 类型性质查询
  consteval size_t rank(info type);
  consteval size_t extent(info type, unsigned i = 0);

  // 类型关系
  consteval bool is_same_type(info type1, info type2);
  consteval bool is_base_of_type(info type_base, info type_derived);
  consteval bool is_virtual_base_of_type(info type_base, info type_derived);
  consteval bool is_convertible_type(info type_src, info type_dst);
  consteval bool is_nothrow_convertible_type(info type_src, info type_dst);
  consteval bool is_layout_compatible_type(info type1, info type2);
  consteval bool is_pointer_interconvertible_base_of_type(info type_base,
                                                          info type_derived);

  template<reflection_range R = initializer_list<info>>
  consteval bool is_invocable_type(info type, R&& type_args);
  template<reflection_range R = initializer_list<info>>
  consteval bool is_invocable_r_type(info type_result, info type, R&& type_args);

  template<reflection_range R = initializer_list<info>>
  consteval bool is_nothrow_invocable_type(info type, R&& type_args);
  template<reflection_range R = initializer_list<info>>
  consteval bool is_nothrow_invocable_r_type(info type_result, info type, R&& type_args);

  // const-volatile 修改
  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 make_signed(info type);
  consteval info make_unsigned(info type);

  // 数组修改
  consteval info remove_extent(info type);
  consteval info remove_all_extents(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);
  template<reflection_range R = initializer_list<info>>
  consteval info common_type(R&& type_args);
  template<reflection_range R = initializer_list<info>>
  consteval info common_reference(R&& type_args);
  consteval info underlying_type(info type);
  template<reflection_range R = initializer_list<info>>
  consteval info invoke_result(info type, R&& type_args);
  consteval info unwrap_reference(info type);
  consteval info unwrap_ref_decay(info type);

  consteval size_t tuple_size(info type);
  consteval info tuple_element(size_t index, info type);
  consteval bool is_applicable_type(info fn, info tuple);
  consteval bool is_nothrow_applicable_type(info fn, info tuple);
  consteval info apply_result(info fn, info tuple);

  consteval size_t variant_size(info type);
  consteval info variant_alternative(size_t index, info type);

  consteval strong_ordering type_order(info type_a, info type_b);
}

std::meta::exception

namespace std::meta {
  class exception : public std::exception
  {
  private:
    optional<string> /*what_*/; // 仅用于阐释
    u8string /*u8what_*/;       // 仅用于阐释
    info /*from_*/;             // 仅用于阐释
    source_location /*where_*/; // 仅用于阐释

  public:
    consteval exception(u8string_view what,
                        info from,
                        source_location where = source_location::current()) noexcept;

    consteval exception(string_view what,
                        info from,
                        source_location where = source_location::current()) noexcept;

    exception(const exception&) = default;
    exception(exception&&) = default;

    exception& operator=(const exception&) = default;
    exception& operator=(exception&&) = default;

    constexpr const char* what() const noexcept override;
    consteval u8string_view u8what() const noexcept;
    consteval info from() const noexcept;
    consteval source_location where() const noexcept;
  };
}

std::meta::access_context

namespace std::meta {
  struct access_context
  {
    access_context() = delete;

    consteval info scope() const;
    consteval info designating_class() const;

    static consteval access_context current() noexcept;
    static consteval access_context unprivileged() noexcept;
    static consteval access_context unchecked() noexcept;
    consteval access_context via(info cls) const;
  };
}

std::meta::member_offset

namespace std::meta {
  struct member_offset
  {
    ptrdiff_t bytes;
    ptrdiff_t bits;
    constexpr ptrdiff_t total_bits() const;
    auto operator<=>(const member_offset&) const = default;
  };
}

std::meta::data_member_options

namespace std::meta {
  struct data_member_options
  {
    struct /*name-type*/
    {                                         // 仅用于阐释
      template<class T>
        requires constructible_from<u8string, T>
      consteval /*name-type*/(T&&);

      template<class T>
        requires constructible_from<string, T>
      consteval /*name-type*/(T&&);

    private:
      variant<u8string, string> /*contents*/; // 仅用于阐释
    };

    optional</*name-type*/> name;
    optional<int> alignment;
    optional<int> bit_width;
    bool no_unique_address = false;
    vector<info> annotations;
  };
}