tcw321
tcw321

Reputation: 113

Why is ranges::split_view not a Bidirectional Range?

I am using the cmcstl2 library with the C++ proposed Ranges with gcc 8

std::string text = "Let me split this into words";
std::string pattern = " ";
auto splitText = text | ranges::view::split(pattern) | 
        ranges::view::reverse;

But this does not work since the view is only a Forward Range not a Bidirectional Range which required by range (which is what I think is going on). Why? if

text | ranges::view::split(pattern)

outputs a view of subranges. Can't that view be reversed?

Also in cmcstl2 I must do the following to print it out.

for (auto x : splitText)
{
    for (auto m : x)
        std::cout << m;
    std::cout << " ";
}

But in range-v3/0.4.0 version I can do:

    for (auto x : splitText)
       std::cout << x << '\n';

Why? What is the type of x?

Upvotes: 4

Views: 343

Answers (1)

Caleth
Caleth

Reputation: 62616

The way it's been written only supports ForwardRange.

You can certainly try to make a BidirectionalRange version, although I suspect that is either hard or less general.

Consider how to specify all the options for pattern such that it can also match backwards.

Upvotes: 1

Related Questions