So I've written a nasty lambda to satisfy a "shortest amount of code necessary to achieve this" question:
values.resize(distance(
begin(values),
remove_if(begin(values), end(values),
[i = 0U, it = cbegin(intervals), end = cend(intervals)](const auto&) mutable {
return it != end && ++i > it->first && (i <= it->second || (++it, true));
})
));
My problem is that on Visual Studio Community 2015 Update 3 version 14.0.25425.01 this outputs the desired:
4.2 9.1 2.3 0.6 6.4 3.6 1.4 7.5
But on all the other compilers I've tried I get:
4.2 2.3 0.6 1.2 0.3 1.4 2.5 7.5
Can anyone tell me what's causing the different behavior?
You are relying on the fact that the exact closure you pass into the algorithm is the one used as the predicate, but the standard allows it to be copied:
This is exactly what libstdc++ does. From v6.2.1:
That call to
std::__find_if
at the start of the function copies__pred
, which means that the value ofi
is incremented a bit withinstd::__find_if
, but this doesn't change what's going on at the call site.To fix this problem, you could use
std::ref
:Live demo