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

Created on 2021-11-29.00:00:00 last changed 1 month ago

Messages

Date: 2021-12-04.13:44:55

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: 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
2021-12-04 13:44:55adminsetmessages: + msg12240
2021-11-29 00:00:00admincreate