kumi v3.1.0
Exquisite Epidote
 
Loading...
Searching...
No Matches
kumi::is_product_type< T, Enable > Struct Template Reference

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

#include <kumi/utils/traits.hpp>

Detailed Description

template<typename T, typename Enable = void>
struct kumi::is_product_type< T, Enable >

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

To be treated like a tuple, an user defined type must supports structured bindings opt-in to kumi::product_type Semantic.

This can be done in two ways:

  • exposing an internal is_product_type type that evaluates to void
  • specializing the kumi::is_product_type traits so it exposes a static constant member value that evaluates to true

Example:

#include <kumi/tuple.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};
kumi::for_each_index( [](int i, auto e)
{
std::cout << "# " << i
<< " : " << e
<< "\n";
}
, peter
);
}
constexpr void for_each_index(Function f, Tuple &&t, Tuples &&... ts)
Applies the Callable object f on each element of a kumi::product_type and its index.
Definition: for_each.hpp:69
KUMI_TRIVIAL_NODISCARD constexpr decltype(auto) get(tuple< Ts... > &&arg) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: tuple.hpp:416
Opt-in traits for types behaving like a kumi::product_type.
Definition: traits.hpp:31