21 Metaprogramming library [meta]

21.4 Reflection [meta.reflection]

21.4.16 Reflection class definition generation [meta.reflection.define.aggregate]

namespace std::meta { struct data_member_options { struct name-type { / exposition only 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; / exposition only }; optional<name-type> name; optional<int> alignment; optional<int> bit_width; bool no_unique_address = false; }; }
The classes data_member_options and data_member_options​::​name-type are consteval-only types ([basic.types.general]), and are not structural types ([temp.param]).
template<class T> requires constructible_from<u8string, T> consteval name-type(T&& value);
Effects: Initializes contents with u8string(std​::​forward<T>(value)).
template<class T> requires constructible_from<string, T> consteval name-type(T&& value);
Effects: Initializes contents with string(std​::​forward<T>(value)).
[Note 1: 
The class name-type allows the function data_member_spec to accept an ordinary string literal (or string_view, string, etc.)
or a UTF-8 string literal (or u8string_view, u8string, etc.)
equally well.
[Example 1: consteval void fn() { data_member_options o1 = {.name = "ordinary_literal_encoding"}; data_member_options o2 = {.name = u8"utf8_encoding"}; } — end example]
— end note]
consteval info data_member_spec(info type, data_member_options options);
Returns: A reflection of a data member description
[Note 2: 
The returned reflection value is primarily useful in conjunction with define_aggregate; it can also be queried by certain other functions in std​::​meta (e.g., type_of, identifier_of).
— end note]
Throws: meta​::​exception unless the following conditions are met:
  • dealias(type) represents either an object type or a reference type;
  • if options.name contains a value, then:
    • holds_alternative<u8string>(options.name->contents) is true and get<u8string>(​options.name->contents) contains the spelling of a valid token that is an identifier ([lex.name]) when interpreted with UTF-8, or
    • holds_alternative<string>(options.name->contents) is true and get<string>(options.name->contents) contains the spelling of a valid token that is an identifier ([lex.name]) when interpreted with the ordinary literal encoding;
    [Note 3: 
    Lexical constructs like universal-character-names ([lex.universal.char]) are not processed.
    For example, R"(\u03B1)" is an invalid identifier and is not interpreted as "α".
    — end note]
  • if options.name does not contain a value, then options.bit_width contains a value;
  • if options.bit_width contains a value V, then
    • is_integral_type(type) || is_enum_type(type) is true,
    • options.alignment does not contain a value,
    • options.no_unique_address is false,
    • V is not negative, and
    • if V equals 0, then options.name does not contain a value; and
  • if options.alignment contains a value, it is an alignment value ([basic.align]) not less than alignment_of(type).
consteval bool is_data_member_spec(info r);
Returns: true if r represents a data member description.
Otherwise, false.
template<reflection_range R = initializer_list<info>> consteval info define_aggregate(info class_type, R&& mdescrs);
Let C be the type represented by class_type and
For every
Constant When:
  • class_type represents a cv-unqualified class type;
  • C is incomplete from every point in the evaluation context;
    [Note 4: 
    C can be a class template specialization for which there is a reachable definition of the class template.
    In this case, the injected declaration is an explicit specialization.
    — end note]
  • is_data_member_spec(
  • is_complete_type(
  • for every pair
Effects: Produces an injected declaration D ([expr.const]) that defines C and has properties as follows:
  • The target scope of D is the scope to which C belongs ([basic.scope.scope]).
  • The locus of D follows immediately after the core constant expression currently under evaluation.
  • The characteristic sequence of D ([expr.const]) is the sequence of reflection values
  • If C is a specialization of a templated class T, and C is not a local class, then D is an explicit specialization of T.
  • For each
  • For every
Remarks: If C is a specialization of a templated class and it has not been instantiated, C is treated as an explicit specialization.

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant