Implement P3349R1 Converting Contiguous Iterators To Pointers by NylteJ · Pull Request #5683 · microsoft/STL (original) (raw)
This PR audits and updates all calls to to_address (or _To_address) to ensure that both to_address(i) and to_address(i + (s - i)) (which should be equivalent to the standard-mandated to_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: ifi + (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.