Skip to content

[unreachable.sentinel] Incorrect usage example of std::unreachable_sentinel #7859

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

Open
xmcgcg opened this issue Apr 29, 2025 · 2 comments
Open
Labels
lwg Issue must be reviewed by LWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.

Comments

@xmcgcg
Copy link
Contributor

xmcgcg commented Apr 29, 2025

An incorrect description of an example was spotted on Stack Overflow (the comment under the question), [unreachable.sentinel] p2 says:

[Example 1 :

char* p;
// set p to point to a character buffer containing newlines
char* nl = find(p, unreachable_sentinel, '\n');

Provided a newline character really exists in the buffer, the use of unreachable_sentinel above potentially makes
the call to find more efficient since the loop test against the sentinel does not require a conditional branch. — end
example
]

The description says that the example is well-defined if a null character is reachable from p, but std::ranges::find requires the input range to be valid, as [iterator.requirements.general] p12 says:

The result of the application of library functions to invalid ranges is undefined.

@jwakely
Copy link
Member

jwakely commented Apr 29, 2025

This is a known issue (e.g. it's mentioned in the comments of LWG4019). The reality is that such infinite ranges do work, there's nothing in the library that actually exhibits undefined behaviour if you write this code. The rule requiring a valid range should probably be relaxed to only require a valid range for at least as many elements as will actually be iterated over. Otherwise there are almost no valid uses of unreachable_sentinel.

@jwakely
Copy link
Member

jwakely commented Apr 29, 2025

I would be opposed to removing the example, because it does show how the unreachable sentinel is intended to be used. The fact that [iterator.requirements.general] is too strict to allow this is a defect, and that's what should be fixed. So not editorial.

@jensmaurer jensmaurer added lwg Issue must be reviewed by LWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking. labels Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lwg Issue must be reviewed by LWG. not-editorial Issue is not deemed editorial; the editorial issue is kept open for tracking.
Projects
None yet
Development

No branches or pull requests

3 participants