Skip to content

Implement LWG-4242 ranges::distance does not work with volatile iterators #5603

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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja commented Jun 21, 2025

Per LWG-4242/WG21-P3742R0 (alternative link).

Fixes #5623.

This PR makes the involved overload of ranges::distance more constrained than explicitly written, because the "Effects: Equivalent to:" in [range.iter.op.distance]/3 isn't changed and we should propagate the implicit constraints per [structure.specifications]/4.

Drive-by change:

  • Removes the skipping for VSO-1898890 (EDGcpfe/26576 EDG produces an error for conversion from array to const reference to pointer) because ranges::distance no longer performs such a conversion.

@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner June 21, 2025 13:48
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Jun 21, 2025
Comment on lines 3604 to +3605
template <class _It, sized_sentinel_for<decay_t<_It>> _Se>
requires _Additionally_subtractable_for_distance<const _Se, _It>
Copy link
Contributor Author

@frederick-vs-ja frederick-vs-ja Jun 21, 2025

Choose a reason for hiding this comment

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

The constraint sized_sentinel_for<decay_t<_It>> seems wrong for volatile iterators. IIUC even when sized_sentinel_for<_Se, decay_t<_It>> is modeled, correct behavior for volatile version of _It is not yet required.

Also, the const for _Se becomes questionable when volatile-qualified _It comes into the picture - as sized_sentinel_for<_Se, decay_t<_It>> isn't requiring anything about volatile-qualified _It.

I guess it's better to replace decay_t<_It> with another type which preserves volatile for non-array _It. CC @hewillk.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why you introduced _additionally_subtractable_for_distance. Could you provide an actual example of it having an effect?

Copy link
Contributor Author

@frederick-vs-ja frederick-vs-ja Jun 23, 2025

Choose a reason for hiding this comment

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

I'm not sure why you introduced _additionally_subtractable_for_distance. Could you provide an actual example of it having an effect?

See the test case - as the explicit constraints uses decay_t, they would be satisfied by volatile reverse_iterator<int*>& and reverse_iterator<int*>. Without _Additionally_subtractable_for_distance there will still be hard error. I believe "Effects: Equivalent to:" doesn't allow such a hard error and actually requires the implementation to add additional constraints.

Godbolt link

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like a LWG to me.

@StephanTLavavej StephanTLavavej added LWG Library Working Group issue ranges C++20/23 ranges labels Jun 24, 2025
@StephanTLavavej StephanTLavavej self-assigned this Jun 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
LWG Library Working Group issue ranges C++20/23 ranges
Projects
Status: Initial Review
Development

Successfully merging this pull request may close these issues.

LWG-4242 ranges::distance does not work with volatile iterators
3 participants