C++ Logo

sg12

Advanced search

Re: [SG12] Fwd: Predicate evaluation order for std::remove_if

From: Jonathan Wakely <cxx_at_[hidden]>
Date: Wed, 21 Oct 2020 17:16:21 +0100
On Wed, 21 Oct 2020 at 17:08, Gabriel Dos Reis via SG12 <
sg12_at_[hidden]> wrote:

> Library groups are more likely to know the answer. Adding them.
>
>
>
> *From:* SG12 <sg12-bounces_at_[hidden]> *On Behalf Of *Eugene Kozlov
> via SG12
> *Sent:* Friday, October 16, 2020 9:34 AM
> *To:* sg12_at_[hidden]
> *Cc:* Eugene Kozlov <nekoeuge_at_[hidden]>
> *Subject:* [SG12] Fwd: Predicate evaluation order for std::remove_if
>
>
>
> Hello.
>
>
>
> std::remove_if doesn't have any specified evaluation order of predicate,
> unlike e.g. std::for_each that is explicitly required to evaluate predicate
> from first to last element in range.
>
>
>
> Is there any reason why it is so?
>
> Do you maybe know any implementation that has a different implementation?
>
> I cannot imagine any sane implementation benefitting from unspecified
> order of evaluation.
>
> It would be nice to have the order explicitly specified in standard.
>
>
>
> In my current task I need a function that sequentially iterates a vector
> and removes some elements based on prior elements values. And apparently I
> cannot use std::remove_if because I cannot be sure that predicate for the
> first element is called first, and so on.
>
>
>
> How dangerous it would be to rely on this implementation detail and use
> std::remove_if for this task?
>
>
>

My recollection is that the order of evaluation is unspecified for most
algorithms, permitting them to be parallelized (in theory) or for other
non-obvious implementation strategies to be allowed.

std::for_each is special, as it's supposed to visit each element in order.
The numerical algos like partial_sum also work in order. But I think it was
intentionally left unspecified for the others.

Received on 2020-10-21 11:16:35