Search code examples
c++std

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


I have a list of filter functions. If any of these functions return true, I should not process an event further.

std::any_of seems suitable for this use case, but I wish to guarantee that the filter functions are called in the order they have been appended to my list (because of possible side effects they may have). Therefore, if I use std::any_of, I need to know the order it calls the filter functions is deterministic, from begin() to end() of the list.

I have checked the C++ standard on std::any_of and the sequential execution policy, but found no mention of order guarantees. I did not find mention about order guarantees on cppreference, and did not find an answered question similar enough on stackoverflow.

My conclusion is that there are no order guarantees. Although most compilers will probably process the functions in my in order, I should not rely on it.

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);
}

Solution

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