-
Notifications
You must be signed in to change notification settings - Fork 255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Throw runtime error for range with last == numeric_limits<T>::max() #1181
Conversation
Seems reasonable. A compile time check would be good for when the bounds are a compile time constant. Another useful check would be |
Thanks! I just saw this after I pushed my fix for this. Our minds think alike -- what you wrote is pretty much exactly one of the options I wrote and considered, before backing it out when I had the idea for the other approach. |
Closing this as the other commit has fixed the issue. Thanks again! |
Wait, on second thought: This could still be complementary, because I still don't support a range whose end value is numeric_limits< [size_t | ptrdiff_t ] >::max(). I think not supporting that case is fine... those are typically used as invalid/sentinel values, not valid program values that you'd want to include in a closed range, I think. But we could throw if they're encountered... |
That's a good idea. I think reverse ranges (i.e. decrementing) might be a useful feature to add to the language, so maybe if @hsutter agrees I can go ahead and start working on implementing those, at which point this check would no longer be necessary. |
Another complementary feature to decrementing ranges is stepped ranges, such as |
Awesome, thanks for the note. |
I'd like to keep the language feature simple and make sure it works well with C++20 ranges and views. I was tempted to add more, including a For example, in Rust for i in (0..100).rev().step_by(5) {
println!("{}", i);
} Seems pretty usable? Would a language feature that complicates the grammar do a lot better? I think the main benefit comes from expressing common things like 0..=100 and first...last better than |
I did some more research on ranges and stepping, and found that there are definitely a lot of varieties out there, so maybe just having the basic syntax for now is best, especially if you can do something like |
Thanks! |
Addresses #1180. The short of the issue is that if we create a numeric range with last == numeric_limits::max(), the
++last
in the constructor causes overflow, which is not great to say the least. I can't think of a clean way to address this issue since a core aspect of c++ iterators is their asymmetry, so I figured it's better just to report an error if we notice a user trying to create an erroneous range. Open to suggestions.