Konstantin Vladimirov
Konstantin Vladimirov

Reputation: 6989

In range v3 library, why ranges::copy do not work with output of ranges::views::chunk?

This works as expected:

#include <range/v3/all.hpp>
#include <iostream>

int main() {
  auto chunklist = ranges::views::ints(1, 13) | ranges::views::chunk(4);
  for (auto rng : chunklist) {
    for (auto elt : rng)
      std::cout << elt << " ";    
    std::cout << std::endl;
  }
}

On the screen:

1 2 3 4
5 6 7 8
9 10 11 12

But if I try to substitute ranges::copy instead of for loop, I am getting compilation error

  auto chunklist = ranges::views::ints(1, 13) | ranges::views::chunk(4);
  for (auto rng : chunklist) {
    ranges::copy(rng, std::ostream_iterator<int>{std::cout, " "}); // FAIL
    std::cout << std::endl;
  }

Compilation error is rather obscure. I will not give full text here but looks like almost all concepts failed:

note:   constraints not satisfied
...
note: within '...' CPP_concept_bool weakly_incrementable =
...
note: within '...' CPP_concept_bool semiregular =
...
note: within '...' CPP_concept_bool default_constructible =
...
note: within '...' CPP_concept_bool constructible_from =
...
confused by earlier errors, bailing out

I am using gcc-9.3 with options:

g++ -fconcepts --std=c++2a chunksimplified.cc

And top of trunk range-v3 library from github

How shall I modify code in order to use explicit algorithm instead of raw loop?

Upvotes: 3

Views: 582

Answers (1)

cigien
cigien

Reputation: 60208

The issue is not with ranges::copy, it's just that ranges::views don't play nicely with std::ostream_iterator.

You can just use ranges::ostream_iterator instead:

ranges::copy(rng, ranges::ostream_iterator<int>{std::cout, " "});  

Here's a working demo.

Upvotes: 3

Related Questions