GCC's <experimental/ranges> filter view does not compile with infinite range iota()

Viewed 404

I'm exploring the experimental ranges library implementation in gcc. When combining an infinite iota range with a filter view, I got a surprising compilation error (live example with GCC 9.0 HEAD 201812):

#include <iostream> 
#include <vector>
#include <experimental/ranges/range>

int main()
{
  using namespace std::experimental::ranges;
  auto odds = view::filter([](int x){ return x%2 != 0; });

  // auto v = std::vector{0,1,2,3,4,5};
  // auto x = v | odds; // (1) ok
  // auto x = view::iota(0,6) | odds; // (2) ok
  // auto x = view::iota(0) | view::take(6); // (3) ok
  auto x = view::iota(0) | view::take(6) | odds; // (4) error: sentinel?

  for (auto e : x) std::cout << e << ' ';
}

What is wrong here?

1 Answers

This is just a bug in cmcstl2. Shorter reproduced example:

auto yes = [](int){ return true; };
// this works
view::filter(view::iota(0), yes);
view::iota(0,10) | view::filter(yes);
// this doesn't 
view::iota(0) | view::filter(yes);

Casey already fixed it in this PR (specifically this commit).

Related