Title
Reversing an infinite range leads to an infinite loop
Status
open
Section
[range.reverse]
Submitter
Barry Revzin

Created on 2023-11-25.00:00:00 last changed 6 days ago

Messages

Date: 2024-11-15.00:00:00

[ 2024-11-14; Related to LWG 4097. ]

Date: 2024-06-28.20:57:21

[ St. Louis 2024-06-28; LWG and SG9 joint session ]

Poll: SG9 and LWG believe this is not a defect?

|SF| F| N| A|SA|
| 3| 3| 0| 2| 0|
Weak consensus => needs to go to LEWG

Poll: SG9 and LWG agree that standard could do something to create fewer sharp edges here, and we encourage a paper exploring options.

|SF| F| N| A|SA|
| 4| 2| 2| 0| 0|

Date: 2024-03-15.00:00:00

[ 2024-03-11; Reflector poll ]

Set priority to 3 after reflector poll. Ask SG9 to look. Probably needs a paper.

Infinite ranges are invalid and giving an invalid range to the library is undefined. But this is not a particularly satisfactory answer given that we provide such ranges ourselves...

Date: 2023-11-27.15:53:35

Consider the following:

auto a = views::iota(0) | views::reverse;
auto b = views::repeat(42) | views::reverse;

Here, views::iota(0) and views::repeat(42) are both non-common bidirectional (even random-access) ranges. They are also infinite ranges, even if the standard doesn't really recognize that.

views::reverse on a non-common range will actually compute the end iterator for you. So while both declarations of a and b above compile, attempting to use either in any way will lead to an infinite loop when you try a.begin() or b.begin().

A reddit post suggested disallowing reversing a non-common range but that likely breaks reasonable use-cases. We could at the very least recognize ranges whose sentinel is unreachable_t and reject those from consideration. For instance, we could change [range.iter.op.next]/3 to Mandate that S is not unreachable_t.

History
Date User Action Args
2024-11-14 11:33:20adminsetmessages: + msg14455
2024-06-28 20:57:21adminsetmessages: + msg14223
2024-03-11 21:49:30adminsetmessages: + msg13978
2024-03-11 21:49:30adminsetstatus: new -> open
2023-11-25 00:00:00admincreate