KUMI v5.0.0
Gorgeous Garnet
Loading...
Searching...
No Matches

◆ from_record

from_record_t<Type> kumi::from_record {}
inlineconstexprnodiscard

Converts a kumi::record to an instance of an arbitrary type that models kumi::record_type.

Constructs an instance of Type by passing elements of r to the appropriate constructor.

Header file

#include <kumi/product_types/record.hpp>

Call Signature

template<typename Type, record_type R>
constexpr auto from_record(R&& r);

Parameters

Return value

  • An instance of Type constructed from each element of r in order.

Example

#include <kumi/kumi.hpp>
#include <iostream>
#include <string>
using namespace kumi::literals;
struct my_struct
{
int i;
float f;
std::string name;
};
template<std::size_t I>
decltype(auto) get(my_struct const & m) noexcept
{
if constexpr (I==0) return kumi::capture_field<"i"_id>(m.i);
if constexpr (I==1) return kumi::capture_field<"f"_id>(m.f);
if constexpr (I==2) return kumi::capture_field<"name"_id>(m.name);
}
template<std::size_t I>
decltype(auto) get(my_struct & m) noexcept
{
if constexpr (I==0) return kumi::capture_field<"i"_id>(m.i);
if constexpr (I==1) return kumi::capture_field<"f"_id>(m.f);
if constexpr (I==2) return kumi::capture_field<"name"_id>(m.name);
}
// Opt-in for Record Type semantic
template<>
inline constexpr bool kumi::is_record_type_v<my_struct> = true;
// Adapt as structured bindable type
template<>
struct std::tuple_size<my_struct> : std::integral_constant<std::size_t,3>
{};
template<> struct std::tuple_element<0,my_struct> { using type = kumi::field<kumi::name<"i"> , int >; };
template<> struct std::tuple_element<1,my_struct> { using type = kumi::field<kumi::name<"f"> , float >; };
template<> struct std::tuple_element<2,my_struct> { using type = kumi::field<kumi::name<"name">, std::string>; };
int main()
{
using namespace kumi::literals;
auto a = kumi::make_record("f"_id=2.3475f, "name"_id="John", "i"_id=1337);
std::cout << a << "\n";
std::cout << b.i << ' ' << b.f << ' ' << b.name << "\n";
}
constexpr bool is_record_type_v
Opt-in traits for types behaving like a kumi::record_type.
Definition traits.hpp:66
decltype(auto) constexpr capture_field(T &&t) noexcept
Creates a field from a given value keeping the qualifiers.
Definition field.hpp:271
Compile-time text based identifier.
Definition identifier.hpp:162
decltype(auto) constexpr get(record< Ts... > &r) noexcept
Extracts the Ith field from a kumi::record.
Definition record.hpp:766