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.

3646. std::ranges::view_interface::size returns a signed type

Section: 26.5.3.1 [view.interface.general] Status: WP Submitter: Jiang An Opened: 2021-11-29 Last modified: 2022-11-17

Priority: 3

View all other issues in [view.interface.general].

View all issues with WP status.

Discussion:

According to 26.5.3.1 [view.interface.general], view_interface::size returns the difference between the sentinel and the beginning iterator, which always has a signed-integer-like type. However, IIUC the decision that a size member function should return an unsigned type by default was made when adopting P1227R2, and the relative changes of the ranges library were done in P1523R1. I don't know why view_interface::size was unchanged, while ranges::size returns an unsigned type in similar situations (26.3.10 [range.prim.size] (2.5)).

If we want to change views_interface::size to return an unsigned type, the both overloads should be changed as below:

constexpr auto size() requires forward_range<D> &&
  sized_sentinel_for<sentinel_t<D>, iterator_t<D>> {
    return to-unsigned-like(ranges::end(derived()) - ranges::begin(derived()));
  }
constexpr auto size() const requires forward_range<const D> &&
  sized_sentinel_for<sentinel_t<const D>, iterator_t<const D>> {
    return to-unsigned-like(ranges::end(derived()) - ranges::begin(derived()));
  }

[2022-01-30; Reflector poll]

Set priority to 3 after reflector poll.

[2022-06-22; Reflector poll]

LEWG poll approved the proposed resolution

[2022-09-23; Reflector poll]

Set status to Tentatively Ready after five votes in favour during reflector poll in July 2022.

[2022-11-12 Approved at November 2022 meeting in Kona. Status changed: Voting → WP.]

Proposed resolution:

This wording is relative to N4901.

  1. Modify 26.5.3.1 [view.interface.general], class template view_interface synopsis, as indicated:

    […]
    constexpr auto size() requires forward_range<D> &&
      sized_sentinel_for<sentinel_t<D>, iterator_t<D>> {
        return to-unsigned-like(ranges::end(derived()) - ranges::begin(derived()));
      }
    constexpr auto size() const requires forward_range<const D> &&
      sized_sentinel_for<sentinel_t<const D>, iterator_t<const D>> {
        return to-unsigned-like(ranges::end(derived()) - ranges::begin(derived()));
      }
    […]