NamedTuple

The class NamedTuple is a data class (like a struct or std::tuple) with string-based read/write access to members. A NamedTuple is built from a list of Fields each having a name and a type:

using Field = dacr::Field<"name", type>;
using NamedTuple = dacr::NamedTuple<Field1, Field2, ..., FieldN>;

The names of the individual fields must be unique within a NamedTuple. Each name must not be empty.

Construction

The NamedTuple type may be constructed with explicit types or via Class-Template-Argument-Deduction (CTAD) of the constructor using initial values.

Explicit Construction

The NamedTuple layout (name/type pairs) is defined expliclty by a list of Fields

using Point3D = dacr::NamedTuple<
    dacr::Field<"x", double>,
    dacr::Field<"y", double>,
    dacr::Field<"z", double>
>;

and finally created at runtime via the constructor by specifying the individual

Point3d point{1.0, -1.0, 0.5};

Implicit Construction via Constructor

Alternatively, the NamedTuple layout maybe created directly via the construtor using either the _field user-defined literal (if not disabled by DACR_DISABLE_FIELD_LITERAL, see Configuration) or the dacr_field helper macro.

auto point3d_by_field_literals = dacr::NamedTuple("x"_field = 1.0, "y"_field = -1.0, "z"_field = 0.5);

auto point3d_by_field_macro = dacr::NamedTuple(dacr_field("x") = 1.0, dacr_field("y") = -1.0, dacr_field("z") = 0.5);

Member Access

The members of the NamedTuple may be accessed for read and write by the get template function:

auto x = point.get<"x">();

point.get<"x">() = 2.0;

Structured Bindings

The NamedTuple may also be used with structured bindings:

auto tuple = dacr::NamedTuple("i"_field = 1, "d"_field = 2.0);

const auto& [i, d] = tuple;