std::any_of 与顺序执行策略一起使用时是否保证迭代顺序?

Does std::any_of guarantee the order of iteration when used with sequencial execution policy?

我有一个过滤函数列表。如果这些功能中的任何一个 return 'true',我不应该进一步处理事件。

std::any_of 似乎适合这个用例,但我希望保证过滤器函数按照它们附加到我的列表中的顺序被调用(因为它们可能有副作用)。 因此,如果我使用 std::any_of,我需要知道它调用过滤器函数的顺序是确定的,从列表的 begin() 到 end()。

我检查了std::any_of 上的C++ 标准和顺序执行策略,但没有提到顺序保证。我没有在 cppreference 上找到关于订单保证的提及,也没有在 Whosebug 上找到足够相似的已回答问题。

我的结论是没有订单保证。虽然大多数编译器可能会按我的顺序处理函数,但我不应该依赖它。

bool MyClass::event(QEvent* event)
{
    std::vector<std::function<bool(QEvent*)> > functionList = getCurrentEventFilters();

    const auto bFilter = std::any_of(functionList .begin(),
                                     functionList .end(),
                                     [&event](std::function<bool(QEvent*)> function) {return function(event);});
    if (bFilter)
    {
        return true;
    }

    return Base::event(event);
}

关于 std::execution::sequenced_policy

The invocations of element access functions in parallel algorithms invoked with this policy (usually specified as std::execution::seq) are indeterminately sequenced in the calling thread.

关于 indeterminately-sequenced

evaluations of A and B are indeterminately sequenced: they may be performed in any order but may not overlap: either A will be complete before B, or B will be complete before A.

对我来说,这似乎是明确声明你不能依赖事物的顺序。
没想到这个..

原则上这对您来说应该无关紧要。在有副作用的东西上调用 std::any_ofstd::all_of 似乎很可疑。

您询问顺序执行策略,但调用了未指定策略的重载。这些可能不同,因为接受执行策略的重载需要 ForwardIterator 而另一个需要 InputIteratorhttp://eel.is/c++draft/alg.any.of.

标准然后在这里说:http://eel.is/c++draft/algorithms.requirements#4.1:

If an algorithm's template parameter is named InputIterator, InputIterator1, or InputIterator2, the template argument shall meet the Cpp17InputIterator requirements.

这基本上意味着实现可能只会递增输入迭代器并且范围将按顺序处理。

相反,我不确定该实现是否会为某些其他类型的迭代器(例如随机访问迭代器)提供额外的算法版本。

相关问题:Why std::transform doesn't guarantee the order (but for_each guarantee the order)? Doesn't this allow trick implementation for performance?。那里的一条评论说:

The implementation is free to detect stronger iterator strengths and apply a different (possibly out-of-order) algorithm.

虽然有一些关于它的讨论。如果为真,则仅当迭代器为输入类型时才能保证顺序。