boostorg / range

Boost.org range module
http://boost.org/libs/range
43 stars 104 forks source link

adjacent_filtered lets the first element entry through #80

Open tonyelewis opened 5 years ago

tonyelewis commented 5 years ago

Following on from Trac#13203

adjacent_filtered always lets the first element of the range leak through before it starts the real filtering. This means it's easy to get it to violate its stated postcondition:

For all adjacent elements [x,y] in the returned range, bi_pred(x,y) is true.

Eg:

int main() {
   const std::vector<int> a = { 0, 1, 2, 3, 4, 5 };

   auto b = a | boost::adaptors::adjacent_filtered( [] (const int &x, const int &y) {
      return ( ( x > 2 ) && ( y > 2 ) );
   } );

   for (const auto &x : b) {
      std::cerr << x << "\n";
   }
}

…outputs:

0
4
5

From what I can see in the code, the predicate is currently only applied in the increment() function, which leaves it too late for the first element to be checked.

tonyelewis commented 5 years ago

This is related to #81.