13

From the documentation of ranges-v3:

view::all

Return a range containing all the elements in the source. Useful for converting containers to ranges.

What makes me confused are:

  1. Under what scenarios are view::all used?
  2. Are standard containers (std::vector, std::list, etc.) not ranges conceptually?

For example:

auto coll = std::vector{ 1, 2, 2, 3 };  
view::all(coll) | view::unique; // version 1
coll | view::unique; // version 2

Is there any difference between version 1 and version 2?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
szxwpmj
  • 465
  • 2
  • 10

1 Answers1

14

Egad, that part of the documentation hasn't been updated since range-v3 switched terminology. Yes, a container is a Range (it has begin() and end() that return an iterator/sentinel pair). It is not a View (a Range with O(1) copy/move). So, the documentation for view::all should read:

view::all

Return a View containing all the elements in the source. Useful for converting containers to Views.

To answer your second question, no there is no difference between version 1 and version 2 in your code.

Eric Niebler
  • 5,927
  • 2
  • 29
  • 43
  • Follow up question: So the main purpose of `views::all` is **to convert** Ranges (not just containers) to Views, in order to (maybe) perform O(1) operations? If not, what's a case where `views::all` actually wraps a non-trivial range? – gonidelis Apr 09 '21 at 09:27
  • 1
    Yes, the purpose of `views::all` is to convert a range to a view so it can be manipulated in O(1). – Eric Niebler Apr 11 '21 at 16:26
  • Could the name may as well be sth like, `views::to_view`? – gonidelis Apr 12 '21 at 00:00