Implement P3349R1 Converting Contiguous Iterators To Pointers #5683
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR audits and updates all calls to
to_address(or_To_address) to ensure that bothto_address(i)andto_address(i + (s - i))(which should be equivalent to the standard-mandatedto_address(i + ranges::distance(i, s))) are evaluated before using the pointer converted from the iterator. Fixes #5295.Additional Notes:
For algorithms accepting an iterator pair
[i, s), this change evaluates onlyto_address(s)instead ofto_address(i + (s - i)).While the proposal specifies converting
[i, s)to[to_address(i), to_address(i + (s - i)))(implying at least oneoperator+invocation before conversion), iterator requirements guarantee thati + (s - i)must be equal tos, and functions likeranges::advancealso do not actually "advance" the iterator in this case.Based on the proposal’s intent, this should not affect iterator validation: if
i + (s - i)is invalid, thensis already invalid and should have been checked before the library call.This could be revised later if necessary.
The constructors of
string_viewandspan, as well as the implementation ofviews::counted, are intentionally unchanged, as the standard explicitly specifies their behavior ([string.view.cons]/9, 13, [span.cons]/6, 11, 19, [range.counted]/2.1). This may be worth submitting as a potential defect.Added some previously missing
static_casts to pass the tests.