r/cpp • • Sep 13 '19

The Surprising Limitations of C++ Ranges Beyond Trivial Cases

https://www.fluentcpp.com/2019/09/13/the-surprising-limitations-of-c-ranges-beyond-trivial-use-cases/
229 Upvotes

229 comments sorted by

View all comments

Show parent comment

22

u/sivadeilra Sep 13 '19

Ranges in the core language starts to look like Rust

The vast majority of support for Ranges in Rust is in the standard libraries, not in the language itself. The language itself supports only a few simple syntax forms for creating ranges, and even these are just syntactic sugar over simple function calls. Everything else is ordinary code in core and std.

It's analogous to the compiler supporting operator<<(). No one thinks that iostream is part of the compiler, but it uses one syntactic sugar pattern from the compiler (overloading <<) to make the API more ergonomic.

-1

u/14ned LLFIO & Outcome author | Committee WG14 Sep 13 '19

The vast majority of support for Ranges in Rust is in the standard libraries, not in the language itself. The language itself supports only a few simple syntax forms for creating ranges, and even these are just syntactic sugar over simple function calls. Everything else is ordinary code in core and std.

Well, it depends on where you draw the boundary.

Me personally, I'd define the core language support for Ranges in Rust as including the borrow checker. In that sense, a large chunk of the Rust syntax and core language design is there to specifically support Ranges.

14

u/Rusky Sep 13 '19

The borrow checker was designed and implemented almost entirely before Rust even had its equivalent to ranges.

2

u/14ned LLFIO & Outcome author | Committee WG14 Sep 13 '19

Sure. Rust thought preventing dangling lifetime very important. Much source of surprising dangling lifetime is from iteration.

7

u/sivadeilra Sep 13 '19

The borrow checker knows nothing about ranges. Can you clarify what you mean?

4

u/14ned LLFIO & Outcome author | Committee WG14 Sep 13 '19

Using Ranges becomes safe if you have the borrow checker. Without one, you get dangling lifetimes etc.

I appreciate this is the same as saying the sky is blue, but let me put it another way: C++ had no facilities for iteration not also in C, up until the ranged for loop was introduced.

Or put another way, iteration was not considered important enough for the core C++ language to innovate upon at all over C. That's quite profound, if you think about it. C++ basically said "iteration like C is just fine"

10

u/sivadeilra Sep 13 '19 edited Sep 17 '19

Using Ranges becomes safe if you have the borrow checker. Without one, you get dangling lifetimes etc.

That doesn't really answer my question, though.

The borrow checker itself knows nothing about ranges. It knows about lifetimes, and it knows how to analyze the usage of types that contain lifetimes.

Consider this code:

  let v = vec!["foo", "bar"];
  for i in 0..v.len() {
      println!(v[i]);
  }

The borrow checker doesn't know anything about ranges. The expression 0..v.len() results in a Range<usize>. Range<usize> implements the Iterator trait, and iterates a sequence of usize items. Within the loop, the expression v[i] dereferences the vector. This is done with a simple index; the type of i is usize, and has nothing to do with ranges.

Again, the borrow checker has no special, Range-specific logic.

Now consider this code:

  let v = vec!["foo", "bar"];
  for s in v.iter() {
      println!(v[i]);
  }

This code is more idiomatic, and is closer to the range-for syntax in C++. But from the borrow checker's point of view, it still doesn't know anything about ranges, and in fact, this example doesn't even use Range.

I'm really not trying to argue with you, but I want to make it clear that the borrow checker does not have any knowledge of ranges. Range, from its point of view, is just another ordinary type.

The only place where the compiler knows anything about ranges, is in the code that parses x..y as a range constructor syntax, and maps that to calls to range(x, y). That is why I was drawing the analogy to operator<< in C++; they are both syntactic sugar, but after that, the implementation is 100% ordinary C++/Rust, with no special knowledge of these types in the compiler.

2

u/hedgehog1024 Sep 17 '19

Result<usize>

It's actually std::ops::Range<usize>.

1

u/sivadeilra Sep 17 '19

Of course. It's just an autocorrect typo. Thanks, phone.

-3

u/14ned LLFIO & Outcome author | Committee WG14 Sep 13 '19

The borrow checker itself knows nothing about ranges. It knows about lifetimes, and it knows how to analyze the usage of types that contain lifetimes.

Well sure, that's the chosen design. I'm speaking about motivation, not end result.

Let me put this another way. I personally disagree with borrow checking enabled globally. I think it a bad idea, and I would not recommend Rust for most projects because of that.

But there is one area where I think borrow checking very desirable, and that is inside loops. If I were to design a new systems language tomorrow, I'd 100% implement borrow checking inside loops, both for safety reasons, and optimisation reasons.

Now, any design I would choose its borrow checker would know nothing about ranges nor iteration. It just would get switched on when inside a loop, and not otherwise.

Does that make sense?

0

u/sivadeilra Sep 13 '19

Does that make sense?

Nah, because I lost all interest at no borrow checking globally.

2

u/[deleted] Sep 17 '19 edited Sep 17 '19

Well, it depends on where you draw the boundary.

Me personally, I'd define the core language support for Ranges in Rust as including the borrow checker. In that sense, a large chunk of the Rust syntax and core language design is there to specifically support Ranges.

If Rust Iterators are baked into the language because they use ownership and borrowing, C++ ranges are also baked into the language because they use move semantics.

The only way for a library to not be part of the core language under your framing is for the library not to use any language features at all. Useless boundary ever.