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

◆ get() [3/4]

template<typename Target , product_type T>
decltype(auto) constexpr get ( T &&  t)
related

Extracts the value with type Target of a kumi::product_type.

Note
This function is not callable on a product_type with duplicate types. It is provided as an utility for User Defined Types, kumi types have their own overloads.
Template Parameters
TargetThe type of the field to extract from the product type.
Tthe type of the product_type from which to the field.
Parameters
tthe product_type from which to the field.
Returns
A references to the values of a kumi::product_type.

Example:

#include <kumi/kumi.hpp>
#include <iostream>
#include <string>
namespace ns
{
struct people
{
std::string name;
int age;
};
template<std::size_t I>
decltype(auto) get(people const& s) noexcept
{
if constexpr(I==0) return s.name;
if constexpr(I==1) return s.age;
}
template<std::size_t I>
decltype(auto) get(people& s) noexcept
{
if constexpr(I==0) return s.name;
if constexpr(I==1) return s.age;
}
}
// Opt-in for Product Type semantic
template<>
struct kumi::is_product_type<ns::people> : std::true_type
{};
// 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 = std::string; };
template<> struct std::tuple_element<1,ns::people> { using type = int; };
int main()
{
ns::people peter{"Peter Parker", 24};
std::cout << kumi::get<std::string> (peter) << "\n";
std::cout << kumi::get<int> (peter) << "\n";
}
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
Opt-in traits for types behaving like a kumi::product_type.
Definition traits.hpp:38