Opt-in traits for types behaving like a kumi::record_type.
To be treated like a record, an user defined type must support structured bindings as well as the kumi::record_type semantic.
#include <kumi/kumi.hpp>
#include <iostream>
#include <string>
using namespace kumi::literals;
namespace ns
{
struct people
{
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;
}
}
template<>
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