Cited from cppref
std::when_any
is defined as follows:
template<class Sequence>
struct when_any_result
{
std::size_t index;
Sequence futures;
};
template<class InputIt>
auto when_any(InputIt first, InputIt last)
-> future<when_any_result<std::vector<
typename std::iterator_traits<InputIt>::value_type>>>;
template<class... Futures>
auto when_any(Futures&&... futures)
-> future<when_any_result<std::tuple<std::decay_t<Futures>...>>>;
What makes me confused is why does the last form not use std::vector
as its result type?
The result contains an index
field, which can be used to retrieve the ready future. To me, std::tuple
is hard to iterate via index
at run-time, but std::vector
is easy to do so.
Is there any rationale behind for such a design?
This InputIt first, InputIt last
is an iterator range. It defines an input range over a sequence of elements of the same type.
This Futures&&... futures
is a variadic template. It defines a series of parameters of potentially different types.
Both of these restrict their input values to some form of future, but the latter one can take different kinds of futures that result in different types of values. A vector<T>
is a homogeneous container; it cannot represent values of different types (unless it's a vector<any>
, but that just makes it cumbersome and inefficient to use).