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:

  1. For algorithms accepting an iterator pair [i, s), this change evaluates only to_address(s) instead of to_address(i + (s - i)).
    While the proposal specifies converting [i, s) to [to_address(i), to_address(i + (s - i))) (implying at least one operator+ invocation before conversion), iterator requirements guarantee that i + (s - i) must be equal to s, and functions like ranges::advance also 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, then s is already invalid and should have been checked before the library call.
    This could be revised later if necessary.
  2. The constructors of string_view and span, as well as the implementation of views::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.
  3. Added some previously missing static_casts to pass the tests.