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

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

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

To me this seems like explicit statement that you cant rely on order of things.
I didnt expect this..

In principle it should not matter to you though. It seems dubious to call std::any_of or std::all_of on something with side effects.


You ask about sequential execution policy but call an overload where no policy is specified. These may differ, since the overload that accepts an execution policy requires ForwardIterator while the other one requires InputIterator: http://eel.is/c++draft/alg.any.of.

The Standard then says here: 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.

This basically means that the implementation may only increment the input iterator and the range will be processed in order.

On the contrary, I am not sure whether the implementation may or may not provide an additional algorithm version for some other type of iterator, such as the random-access one.

A relevant question: Why std::transform doesn't guarantee the order (but for_each guarantee the order)? Doesn't this allow trick implementation for performance?. One comment there says:

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

Though there is some discussion about it. If it is true, the order is guaranteed only if the iterator is of input type.

Tags:

C++

Std