tupe/value: add transform_array

This commit is contained in:
Danny Robson 2022-02-09 12:51:02 +10:00
parent 6fb04e7fb6
commit d096eb9d21

View File

@ -245,4 +245,45 @@ namespace cruft::tuple::value {
std::forward<TupleT> (val)
);
}
///////////////////////////////////////////////////////////////////////////
namespace detail {
template <typename FunctionT, typename TupleT, size_t ...Index>
requires (
std::is_convertible_v<
std::tuple_element_t<Index, TupleT>,
std::common_type<
std::tuple_element_t<Index, TupleT>...
>
> && ...
)
auto
transform_array (index::indices<Index...>, FunctionT &&func, TupleT &&val)
{
using value_type = std::common_type<
std::tuple_element_t<Index, TupleT>...
>;
return std::array<value_type, std::tuple_size_v<TupleT>> (
std::invoke (func, std::get<Index> (val))...
);
}
}
///------------------------------------------------------------------------
/// Transform each element of a tuple with the supplied function and
/// return an array of the return values.
///
/// The function must return types that have a computable common type.
template <typename FunctionT, typename TupleT>
auto
transform_array (FunctionT &&func, TupleT &&val)
{
return transform_array (
std::forward<FunctionT> (func),
std::forward<TupleT> (val)
);
}
}