Title
std::ranges::view_interface::size returns a signed type
Status
c++23
Section
[view.interface.general]
Submitter
Jiang An

Created on 2021-11-29.00:00:00 last changed 13 months ago

Messages

Date: 2022-11-17.00:42:33

Proposed resolution:

This wording is relative to N4901.

  1. Modify [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()));
      }
    […]
    
Date: 2022-11-12.00:00:00

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

Date: 2022-09-15.00:00:00

[ 2022-09-23; Reflector poll ]

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

Date: 2022-06-15.00:00:00

[ 2022-06-22; Reflector poll ]

LEWG poll approved the proposed resolution

Date: 2022-01-15.00:00:00

[ 2022-01-30; Reflector poll ]

Set priority to 3 after reflector poll.

Date: 2021-11-29.00:00:00

According to [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 ([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()));
  }
History
Date User Action Args
2023-11-22 15:47:43adminsetstatus: wp -> c++23
2022-11-17 00:42:33adminsetmessages: + msg13052
2022-11-17 00:42:33adminsetstatus: voting -> wp
2022-11-08 03:46:49adminsetstatus: ready -> voting
2022-09-23 14:31:54adminsetmessages: + msg12782
2022-09-23 14:31:54adminsetmessages: + msg12781
2022-09-23 14:31:54adminsetstatus: lewg -> ready
2022-01-30 17:05:36adminsetmessages: + msg12322
2022-01-30 17:05:36adminsetstatus: new -> lewg
2021-12-04 13:44:55adminsetmessages: + msg12240
2021-11-29 00:00:00admincreate