8

For the following code:

#include <iostream>
#include <string>
#include <ranges>

int main() 
{
    std::string s = " text ";
    auto sv = std::ranges::views::split(s, ' ');                    
    std::cout << std::ranges::distance(sv.begin(), sv.end());
}

the output is 2. The empty sub-range after the last delimiter is not present in the output range.

This seems inconsistent, since I expect there to be N+1 sub-ranges in the output range if there are N occurrences of the delimiter in the input range. Why is this not the case?

Note that range-v3 does exactly the same thing, so I'm sure this is intentional, but I'd like to know why.

Fureeish
  • 12,533
  • 4
  • 32
  • 62
cigien
  • 57,834
  • 11
  • 73
  • 112
  • 1
    If I had to guess, I'd say that split will take all characters _up until_ the delimiter. So, since you have a delimiter at the beginning of the string, you will get all characters that come before it (in this case, no characters). – paddy Aug 20 '20 at 03:47
  • From what I understand from [draft](http://eel.is/c++draft/range.split#outer-6), it seems expected :-/ – Jarod42 Aug 20 '20 at 09:11
  • 1
    @Jarod42 Yes, but doesn't that seem odd? I'm trying to write some code, and stumbled upon this (inconsistency?), and having to work around this is unpleasant. – cigien Aug 20 '20 at 13:19
  • @cigien: It probably makes a lot more sense when you're splitting ranges of arbitrary objects, rather than processing string ranges. – Nicol Bolas Aug 20 '20 at 15:49
  • @NicolBolas I'm not sure what you mean. What's the difference when splitting arbitrary ranges? Is the behavior different? – cigien Aug 20 '20 at 16:09
  • How would you implement iterator of that view and more particularly `end()`? Handling this special case seems to require extra space/treatment. As I saw, there is already special case for empty separator, where each character is a sub-range (and we don't have empty subrange at start or end). – Jarod42 Aug 20 '20 at 21:20
  • 6
    Submitted [LWG 3478](https://cplusplus.github.io/LWG/issue3478). This just seems obviously wrong to me. – Barry Aug 23 '20 at 17:15
  • 1
    @Barry Oh, very nice, thanks. Yeah, seems wrong, or at least weirdly inconsistent. I didn't think of comparing to other languages, that's a good point. – cigien Aug 23 '20 at 17:20
  • Now, gcc (truck) already contains a fix and prints 3: https://godbolt.org/z/vTGsdo91q – Fedor Oct 23 '21 at 19:55
  • 1
    @Fedor Thanks again :) I should probably, at some point, go through my own questions about compiler variance, and see which, if any, have been resolved. – cigien Oct 23 '21 at 20:19
  • What do we think should happen with an empty string here, versus that Issue? Will we get an empty set of ranges back, as I currently do - or should we get a set of one empty string? – underscore_d Jan 14 '22 at 10:01

0 Answers0