Issue 4242: ranges::distance does not work with volatile iterators (original) (raw)


This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of WP status.

4242. ranges::distance does not work with volatile iterators

Section: 24.4.4.3 [range.iter.op.distance] Status: WP Submitter: Hewill Kang Opened: 2025-04-12 Last modified: 2025-06-23

Priority: Not Prioritized

View all other issues in [range.iter.op.distance].

View all issues with WP status.

Discussion:

After LWG 3664(i), ranges::distance computes the distance between last and firstby returning last - static_cast<const decay_t<I>&>(first) when the two are subtractable. However, this will cause a hard error if first is volatile-qualified (demo):

#include

int main() { int arr[] = {1, 2, 3}; int* volatile ptr = arr; // return std::distance(ptr, arr + 3); // this is ok return std::ranges::distance(ptr, arr + 3); // hard error }

The resolution changes the Effects of LWG 3664(i) from "cute" to "noncute".

[2025-06-12; Reflector poll]

Set status to Tentatively Ready after eight votes in favour during reflector poll.

"Would prefer auto(first) but the current wording is correct."

[Sofia 2025-06-21; Status changed: Voting → WP.]

Proposed resolution:

This wording is relative to N5008.

  1. Modify 24.4.4.3 [range.iter.op.distance] as indicated:

    template<class I, sized_sentinel_for<decay_t> S>
    constexpr iter_difference_t<decay_t> ranges::distance(I&& first, S last);

    -3- Effects: Equivalent to:

    if constexpr (!is_array_v<remove_reference_t>)
    return last - first;
    else
    return last - static_cast<const decay_t&>(first);