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

◆ apply

apply_t kumi::apply {}
inlineconstexprnoexcept

Callable object invoking the callable object f with the elements of the product type unrolled as arguments.

f is applied on the underlying values when the input t is a record type.

Note
This function does not take part in overload resolution if f can't be applied to the elements of t.

Header file

#include <kumi/algorithm/apply.hpp>

Call Signature

template<typename Function, product_type T>
constexpr decltype(auto) apply(Function && f, T && t) noexcept;
constexpr apply_t apply
Callable object invoking the callable object f with the elements of the product type unrolled as argu...
Definition apply.hpp:99

Parameters

  • f: Callable object to be invoked
  • t: Product Type whose elements are used as arguments to f

Return value

  • The value returned by f.

Helper type

template<typename Function, kumi::concepts::product_type T> struct apply
{
using type = decltype(kumi::apply(std::declval<Function>(), std::declval<T>()));
};
template<typename Function, kumi::concepts::product_type T> using apply_t = typename apply<Function, T>::type;

Computes the return type of a call to kumi::apply

Examples

  • Tuple
    #include <kumi/kumi.hpp>
    #include <iostream>
    template<kumi::concepts::product_type Tuple>
    void print(std::ostream& os, Tuple const& t)
    {
    (
    [&os](auto const&... args)
    {
    os << '[';
    std::size_t n{0};
    ((os << args << (++n != kumi::size<Tuple>::value ? ", " : "")), ...);
    os << ']';
    }, t
    );
    os << '\n';
    }
    int main()
    {
    auto t = kumi::tuple{1,2.,3.f};
    // Simple operation: sum all values
    std::cout << kumi::apply( [](auto... m) { return (m + ...); }, t) << "\n";
    // Advanced usage
    print(std::cout, t);
    }
    Computes the number of elements of a kumi::product_type.
    Definition traits.hpp:120
    Fixed-size collection of heterogeneous values.
    Definition tuple.hpp:33
  • Record
    #include <kumi/kumi.hpp>
    #include <iostream>
    template<kumi::concepts::record_type Record>
    void print(std::ostream& os, Record const& t)
    {
    (
    [&os](auto const&... args)
    {
    os << '[';
    std::size_t n{0};
    ((os << args << (++n != kumi::size<Record>::value ? ", " : "")), ...);
    os << ']';
    }, t
    );
    os << '\n';
    }
    int main()
    {
    using namespace kumi::literals;
    auto r = kumi::record{"x"_id = 1, "y"_id = 2., "z"_id = 3.f};
    // Simple operation: sum all values
    std::cout << kumi::apply( [](auto... m) { return (m + ...); }, r) << "\n";
    // Advanced usage
    print(std::cout, r);
    }
    Fixed-size collection of heterogeneous tagged fields, tags are unique.
    Definition record.hpp:36