2

In the following code,

std::vector<int> vectors[] = {{1, 2, 3}, {4, 5}};
auto view = vectors | std::views::transform([](auto v) { return v; })
                    | std::views::join;

why the result of the first pipe is not a range adaptor closure and cannot be passed to a subsequent pipe?

The error snippet:

error: no match for 'operator|' (operand types are 'std::ranges::transform_view<std::ranges::ref_view<const std::vector<int> [2]>, run()::<lambda(auto:27)> >' and 'const std::ranges::views::_Join')
  147 |   auto view = vectors | std::views::transform([](auto v) { return v; }) | std::views::join;
      |               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~
      |                       |                                                               |
      |                       |                                                               const std::ranges::views::_Join
      |                       std::ranges::transform_view<std::ranges::ref_view<const std::vector<int> [2]>, run()::<lambda(auto:27)> >
...
note:   'std::ranges::views::__adaptor::_RangeAdaptorClosure' is not a base of 'std::ranges::transform_view<std::ranges::ref_view<const std::vector<int> [2]>, run()::<lambda(auto:27)> >'
   67 |     concept derived_from = __is_base_of(_Base, _Derived)
      |                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
...
Seriy
  • 43
  • 3
  • 1
    See [p2328r0](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/p2328r0.html). – 康桓瑋 Jun 27 '21 at 12:52
  • 1
    Because the elements of this `transform_view` are temporaries that are not views. See https://stackoverflow.com/a/39810436. A proposal was accepted this month that makes this case Just Work (https://wg21.link/P2328R1), but implementers need time to implement it. – cpplearner Jun 27 '21 at 12:52
  • @cpplearner. gcc-trunk has been implemented. – 康桓瑋 Jun 27 '21 at 12:53
  • Thank you, just don't track the ongoing development. Do I understand correctly that once implemented such views can be iterated through only once? As per the paper: > Since join_view in this case is an input range, begin can only be called once. After begin is called, the join_view object can no longer be used as a range, let alone a view, so the fact that destroying or moving from it may require destruction of the cached elements is irrelevant. – Seriy Jun 27 '21 at 13:53
  • 1
    @Seriy. yes, it can only be iterated once, see this [example](https://godbolt.org/z/r8aq4aKjh) – 康桓瑋 Jun 27 '21 at 15:03
  • @康桓瑋 didn't know this simple way to try out the gcc-trunk, thanks. – Seriy Jun 27 '21 at 15:49

0 Answers0