Given an std::tuple
-like object (i.e. with defined tuple_size
and get
semantics) and a unary functor object ftor
, I want to be able to call ftor
on each element of the tuple
-like object.
If I disregard the return value, I am aware of the int array trick:
namespace details {
template <typename Ftor, typename Tuple, size_t... Is>
void apply_unary(Ftor&& ftor, Tuple&& tuple, std::index_sequence<Is...>) {
using std::get;
int arr[] = { (ftor(get<Is>(std::forward<Tuple>(tuple))), void(), 0)... };
}
} // namespace details
template <typename Ftor, typename Tuple>
void apply_unary(Ftor&& ftor, Tuple&& tuple) {
details::apply_unary(std::forward<Ftor>(ftor),
std::forward<Tuple>(tuple),
std::make_index_sequence<std::tuple_size<Tuple>::value> {});
}
If I want the return values, I could replace the int []
trick with a call to std::make_tuple
instead and return that. That would work provided that none of the calls to the ftor
object have a void
return value...
The question I have is therefore: considering I want to get the results of the call, how can I handle calls that might return void
?
The only requirement is that I should get the results as a tuple and be able to tell which call lead to which element of the said result tuple.