KUMI v4.0.0
Flawless Fluorite
Loading...
Searching...
No Matches

◆ is_record_type_v

template<typename T>
bool kumi::is_record_type_v = requires { typename T::is_record_type; } && kumi_implementation_defined
inlineconstexpr

Opt-in traits for types behaving like a kumi::record_type.

Template Parameters
TType to inspect

To be treated like a record, an user defined type must support structured bindings as well as the kumi::record_type semantic.

This can be done in two ways:

  • exposing an internal is_record_type type that evaluates to void
  • specializing the kumi::is_record_type_v inline variable so that it evaluates to true for the type

Helper type-trait

namespace kumi
{
template<typename T> struct is_record_type;
}
Main KUMI namespace.
Definition algorithm.hpp:11

Example:

#include <kumi/kumi.hpp>
#include <iostream>
#include <string>
using namespace kumi::literals;
namespace ns
{
struct people
{
std::string name;
int age;
};
template<kumi::concepts::identifier auto ID>
decltype(auto) get(people const& s) noexcept
{
if constexpr(ID == "name"_id) return s.name;
if constexpr(ID == "age"_id) return s.age;
}
template<kumi::concepts::identifier auto ID>
decltype(auto) get(people& s) noexcept
{
if constexpr(ID == "name"_id) return s.name;
if constexpr(ID == "age"_id) return s.age;
}
}
// Opt-in for Record Type semantic
template<>
inline constexpr bool kumi::is_record_type_v<ns::people> = true;
// Adapt as structured bindable type
template<>
struct std::tuple_size<ns::people>
: std::integral_constant<std::size_t,2> {};
template<> struct std::tuple_element<0,ns::people> { using type = kumi::field<kumi::name<"name">,std::string>; };
template<> struct std::tuple_element<1,ns::people> { using type = kumi::field<kumi::name<"age"> ,int >; };
int main()
{
ns::people peter{"Peter Parker", 24};
kumi::for_each_field( [](auto n, auto e)
{
std::cout << "# " << n
<< " : " << e
<< "\n";
}
, peter
);
}
constexpr bool is_record_type_v
Opt-in traits for types behaving like a kumi::record_type.
Definition traits.hpp:66
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:618