close
Namespaces
Variants

std::meta::data_member_spec

From cppreference.com
< cpp | meta
 
 
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11)(DR*)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11) 
(C++11)
(C++11)
Type properties
(C++11)
(C++11)
(C++14)
(C++11)(deprecated in C++26)
(C++11)(until C++20*)
(C++11)(deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
Relationships and property queries
Type modifications
(C++11)(C++11)(C++11)
Type transformations
(C++11)(deprecated in C++23)
(C++11)(deprecated in C++23)
(C++11)
(C++11)(until C++20*)(C++17)

(C++11)
(C++17)
Compile-time rational arithmetic
Compile-time integer sequences
 
Defined in header <meta>
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 ⊥) if options.name doesn't contain a value.
  • A is the alignment value held by options.alignment, or a special value (denoted by ⊥) if options.alignment doesn't contain a value.
  • W is the value held by options.bit_width, or a special value (denoted by ⊥) if options.bit_width doesn'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 each r in options.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.name contains 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.name does not contain a value, options.bit_width contains a value. (In this case, the result describes an unnamed bit-field.)
  • If options.bit_width contains a value V, then:
  • std::meta::is_integral(type) || std::meta::is_enum_type(type) is true.
  • options.alignment contains a value.
  • options.no_unique_address is false.
  • V is not negative.
  • If V is zero, options.name does not contain a value.
  • If options.alignment contains a value, it is a valid alignment value and is not less than std::meta::alignment_of(type).
  • For each element r in options.annotations, std::meta::type_of(r) is valid and represents a non-array object type, and evaluation of std::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;
        int i = 0;
        template for (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:

Hello, C++26!

See also