std::meta::data_member_spec - cppreference.com
From cppreference.com
| Defined in header |
||
consteval std::meta::info data_member_spec( std::meta::info type, std::meta::data_member_options options ); |
(1) | (since C++26) |
struct data_member_options { std::optional</*name-type*/> name; std::optional<int> alignment; std::optional<int> bit_width; bool no_unique_address = false; std::vector<std::meta::info> annotations; }; |
(2) | (since C++26) |
1) Returns a reflection of a data member description, which can be reified as a data member of a class using
std::meta::define_aggregate.
2) The parameter type data_member_options is an aggregate type. It is not a structural type, and objects of this type can only be used at compile time.
A data member description is usually represented as a sextuple (T, N, A, W, NUA, ANN), where:
- T is the type represented by
std::meta::dealias(type). - N is either the identifier encoded by
options.name, or a special value (denoted by ⊥) ifoptions.namedoesn't contain a value. - A is the alignment value held by
options.alignment, or a special value (denoted by ⊥) ifoptions.alignmentdoesn't contain a value. - W is the value held by
options.bit_width, or a special value (denoted by ⊥) ifoptions.bit_widthdoesn't contain a value. - NUA is the value of
options.no_unique_address. - ANN is the sequence of values
std::meta::constant_of(r)for eachrinoptions.annotations.
Two data member descriptions are equal if each of their respective components are the same entities, are the same identifiers, have equal values, or are both ⊥. Two reflections of data member descriptions compare equal if the descriptions they represent are equal.
If the combination of arguments is invalid, std::meta::exception is thrown. See the Exceptions section below for details.
The exposition-only type designated by /*name-type*/ is a class type with the following constructors:
std::meta::data_member_options::name-type::name-type
template< class T > requires std::constructible_from<std::u8string, T> consteval /*name-type*/( T&& value ); |
(1) | |
template< class T > requires std::constructible_from<std::string, T> consteval /*name-type*/( T&& value ); |
(2) | |
1) Constructs the name from the UTF-8 string std::u8string(std::forward<T>(value)).
2) Constructs the name from the string std::string(std::forward<T>(value)) in the ordinary literal encoding.
Parameters
| type | - | the type of the data member to define |
| options | - | other features of the data member to define |
Return value
A reflection that represents a data member description, constructed from the arguments.
Exceptions
Throws std::meta::exception unless all of the following conditions are met:
std::meta::dealias(type)represents an object type or a reference type.- If
options.namecontains a value:
- If the name was constructed from a UTF-8 string, the string is the spelling of a valid token, and the token is a valid identifier, when interpreted with UTF-8.
- Otherwise (the name was constructed from an ordinary string), the string is the spelling of a valid token, and the token is a valid identifier, when interpreted with the ordinary literal encoding.
- If
options.namedoes not contain a value,options.bit_widthcontains a value. (In this case, the result describes an unnamed bit-field.) - If
options.bit_widthcontains a value V, then:
std::meta::is_integral(type) || std::meta::is_enum_type(type)is true.options.alignmentcontains a value.options.no_unique_addressis false.- V is not negative.
- If V is zero,
options.namedoes not contain a value.
- If
options.alignmentcontains a value, it is a valid alignment value and is not less thanstd::meta::alignment_of(type). - For each element
rinoptions.annotations,std::meta::type_of(r)is valid and represents a non-array object type, and evaluation ofstd::meta::constant_of(r)does not throw an exception.
Example
#include <format> #include <meta> #include <print> #include <string> // Defines struct A as if by “struct A { std::string x; };” struct A; consteval { std::meta::define_aggregate(^^A, { std::meta::data_member_spec(^^std::string, {.name = "x"}), }); } // Defines a simple tuple type template<typename... Ts> struct Tuple { struct Storage; consteval { std::vector<std::meta::info> member_specs; template for (int i{}; auto type : {^^Ts...}) { member_specs.push_back(data_member_spec(type, {.name = std::format("item{}", i)})); ++i; } define_aggregate(^^Storage, member_specs); } Storage data; Tuple(): data{} {} Tuple(const Ts& ...values): data{values...} {} }; int main() { A a; a.x = "Hello"; Tuple<char, char, char, int> t('C', '+', '+', 26); std::println("{}, {}{}{}{}!", a.x, t.data.item0, t.data.item1, t.data.item2, t.data.item3); }
Output: