为什么 std::when_any 使用 std::tuple 而不是 std::vector 作为其结果类型?

Why does std::when_any use std::tuple rather than std::vector as its result type?

引用自cppref

std::when_any定义如下:

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>...>>>;

令我困惑的是为什么最后一个表格不使用 std::vector 作为其结果类型?

结果包含一个index字段,可用于检索就绪的未来。对我来说,std::tuple 很难在 运行 时通过 index 迭代,但是 std::vector 很容易做到。

这样的设计有什么道理吗?

这个InputIt first, InputIt last是一个迭代器范围。它定义了一系列相同类型元素的输入范围。

这个 Futures&&... futures 是一个可变参数模板。它定义了一系列可能不同类型的参数。

这两者都将它们的输入值限制为某种形式的未来,但后者可以采用不同类型的未来,从而产生不同类型的值。 A vector<T> 是均质容器;它 不能 表示不同类型的值(除非它是 vector<any>,但这只会使其使用起来麻烦且效率低下)。