kumi v3.1.0
Exquisite Epidote
 
Loading...
Searching...
No Matches

◆ to_record()

template<record_type Type>
constexpr auto kumi::to_record ( Type &&  r)
inlineconstexpr

Converts a kumi::record_type to an instance kumi::record.

Constructs an instance kumi::record from the elements of the kumi::product_type parameters

Parameters
rkumi::product_type to convert
Returns
An instance of kumi::record constructed from each elements of t in order.

Example

#include <kumi/kumi.hpp>
#include <cstdint>
#include <iostream>
#include <type_traits>
#include <utility>
struct pixel
{
int r, g, b;
};
template<std::size_t I>
decltype(auto) get(pixel const& p) noexcept
{
if constexpr(I==0) return kumi::capture_field<"r">(p.r);
if constexpr(I==1) return kumi::capture_field<"g">(p.g);
if constexpr(I==2) return kumi::capture_field<"b">(p.b);
}
template<std::size_t I>
decltype(auto) get(pixel& p) noexcept
{
if constexpr(I==0) return kumi::capture_field<"r">(p.r);
if constexpr(I==1) return kumi::capture_field<"g">(p.g);
if constexpr(I==2) return kumi::capture_field<"b">(p.b);
}
// Opt-in for Record Type semantic
template<>
struct kumi::is_record_type<pixel> : std::true_type
{};
// Adapt as structured bindable type
template<>
struct std::tuple_size<pixel> : std::integral_constant<std::size_t,3>
{};
template<> struct std::tuple_element<0,pixel> { using type = kumi::field_capture<"r", int>; };
template<> struct std::tuple_element<1,pixel> { using type = kumi::field_capture<"g", int>; };
template<> struct std::tuple_element<2,pixel> { using type = kumi::field_capture<"b", int>; };
int main()
{
pixel a = { 37, 15, 27 };
auto b = kumi::to_record(a);
std::cout << b << "\n";
}
constexpr auto to_record(Type &&r)
Converts a kumi::record_type to an instance kumi::record.
Definition convert.hpp:146
constexpr decltype(auto) get(record< Ts... > &&r) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition record.hpp:395
Named wrapper over a type.
Definition field_capture.hpp:29
Opt-in traits for types behaving like a kumi::product_type.
Definition traits.hpp:61