I was looking through this tuple for_each()
implementation a few months ago and was wondering if it is possible to implement a version that collects the return values of invoking the functions into a tuple as a result?
The reason I want to do this in my code base I have the following function which takes an input a variadic list of shapes, and returns a tuple of values.
template <typename... T, typename... R>
static constexpr auto map_to_opengl(T &&... shapes)
{
return std::make_tuple(shape_mapper::map_to_array_floats(shapes)...);
}
Well, I'd like to change my function signature to accept a tuple of shapes, and return the result of invoking the function on each shape (this should be semantically equivalent to the code above). If I can do this, I can keep my code more DRY, which is important to me.
template <typename... T, typename... R>
static constexpr auto map_to_opengl(std::tuple<T...> &&shapes)
{
return tuple_foreach(shapes, &shape_mapper::map_to_array_floats);
}
However the implementation of tuple_foreach
doesn't allow any values to be collected. Is it possible to write such a function? If it exists in Hana, I missed it :(
I guess you wouldn't call this algorithm for_each, but maybe accumulate? I'm not sure here.
Nothing was added to the standard library in C++17 that would particularly help here (that I can think of); here's the usual C++14 approach (using pack expansion) as a standalone algorithm:
Online Demo