This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of SG9 status.

4019. Reversing an infinite range leads to an infinite loop

Section: 26.7.21 [range.reverse] Status: SG9 Submitter: Barry Revzin Opened: 2023-11-25 Last modified: 2024-03-11

Priority: 3

View all other issues in [range.reverse].

View all issues with SG9 status.

Discussion:

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 25.4.4.4 [range.iter.op.next]/3 to Mandate that S is not unreachable_t.

[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...

Proposed resolution: