Skip to content
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

[iterator.operations] Clarify the observable side-effects of distance. #2813

Closed
wants to merge 3 commits into from

Conversation

umanwizard
Copy link

@umanwizard umanwizard commented Mar 30, 2019

The standard claims that for non-Random-Access iterators, distance "uses ++". I guess the reader is supposed to infer from this that you can't continue using an iterator that doesn't have a multi-pass guarantee after passing it to distance, but it could be more clear. Especially since distance(a, a) won't actually invalidate anything (at any rate, not in libc++ and I'd be quite surprised if it did in any standard library implementation)

I believe that this lack of clarity in the standard is an editorial oversight, and that the committee's intended meaning is that distance(first, last) has the same observable side-effects as calling pre-increment ++ on first until it equals last.

Fixes #2807.

Copy link
Member

@jwakely jwakely left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's an oversight, I think the implication is already there.

Why introduce i instead of incrementing first directly?

@umanwizard
Copy link
Author

umanwizard commented Mar 30, 2019

@jwakely Sure, no good reason to introduce a separate i. I can change it.

I don't see how the implication is clear? It seems to say that distance will just return the number of necessary increments to get from point a to point b, not that it will necessarily perform them.

Let's say I make an iterator that has some side-effect like printing hello world every time it's incremented. And let's say it does not expose the interface of a random-access iterator, but the compiler and/or stdlib are able to figure out that it does in fact point into a random-access structure, and optimize away the increments to return the correct value from distance but avoid printing hello world.

Is that conforming? I think according to the standard as currently written, it is: the standard just says distance returns the number of increments required, not that it actually performs them. But I think according to the clear design intent, it wasn't meant to be.

@jwakely
Copy link
Member

jwakely commented Mar 31, 2019

But the standard doesn't say that magic, impossible optimisation happens, so you can't assume it does, and have to assume the increments happen, and so other copies of input iterators can be affected.

Saying "no impossible magic happens" doesn't seem necessary to me. A note stating that input iterators might become non-dereferenceable might be reasonable, but specifying the function using code isn't an improvement IMHO.

@umanwizard
Copy link
Author

But the standard doesn't say that magic, impossible optimisation happens, so you can't assume it does

Right, but it also doesn't state that the increments do happen, as far as I can tell.

@jensmaurer
Copy link
Member

I'm not convinced the rewrite as code is a net improvement. I do sympathize with the sentiment that "needed to get" is sloppy wording of sorts; is there maybe a small prose adjustment that helps here? "increments first until last is reached and returns the number of increments" or so?

@zygoloid zygoloid added the decision-required A decision of the editorial group (or the Project Editor) is required. label Oct 7, 2019
@jensmaurer
Copy link
Member

Editorial meeting: Go with Jens' suggestion.

@jensmaurer jensmaurer added changes requested Changes to the wording or approach have been requested and not yet applied. and removed decision-required A decision of the editorial group (or the Project Editor) is required. labels Oct 21, 2019
@CaseyCarter
Copy link
Contributor

Please be sure to keep the wording style consistent in std::advance, std::distance, ranges::advance, and ranges::distance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changes requested Changes to the wording or approach have been requested and not yet applied.
Projects
None yet
5 participants