3739. chunk_view::size should preserve the signedness of the size of the underlying range

Section: 25.7.29.2 [range.chunk.view.input], 25.7.29.6 [range.chunk.view.fwd] Status: NAD Submitter: Hewill Kang Opened: 2022-07-15 Last modified: 2022-11-30

Priority: Not Prioritized

View all other issues in [range.chunk.view.input].

View all issues with NAD status.

Discussion:

Currently, the Effects of chunk_view::size simply returns to-unsigned-like(div-ceil(ranges::distance(base_), n_)), where div-ceil is defined in 25.7.29.2 [range.chunk.view.input] as:

template<class I>
constexpr I div-ceil(I num, I denom) { // exposition only
  I r = num / denom;
  if (num % denom)
    ++r;
  return r;
}

There are two problems here. First, for the const version of chunk_view::size, the types of ranges::distance(base_) and n_ are range_difference_t<const V> and range_difference_t<V> respectively, and the two parameters of div-ceil have the same type I. Given that the standard does not guarantee that V and const V must have the same difference_type, this makes the div-ceil's template deduction fail when the two are different.

Second, the standard does not guarantee that ranges::size must return an unsigned type, but here we use to-unsigned-like to unconditionally convert the return type of chunk_view::size to an unsigned type, which is inconsistent with the behavior of other range adaptors such as take_view and drop_view.

We should try to preserve the characteristics of the range_size_t of the underlying range as much as possible.

[2022-08-23; Reflector poll: NAD (would need a paper for LEWG)]

The "range_difference_t<const V> and range_difference_t<V> can be both valid but have different types" part is something I expect Mr. Carter's eventual paper to outlaw. The "preserve signedness" part is LEWG.

[2022-11-30 LWG telecon. Status changed: Tentatively NAD → NAD.]

Proposed resolution:

This wording is relative to N4910.

  1. Modify 25.7.29.2 [range.chunk.view.input] as indicated:

    constexpr auto size() requires sized_range<V>;
    constexpr auto size() const requires sized_range<const V>;
    

    -5- Effects: Equivalent to:

    return to-unsigned-like(div-ceil(ranges::sizedistance(base_), static_cast<decltype(ranges::size(base_))>(n_));
    

  2. Modify 25.7.29.6 [range.chunk.view.fwd] as indicated:

    constexpr auto size() requires sized_range<V>;
    constexpr auto size() const requires sized_range<const V>;
    

    -3- Effects: Equivalent to:

    return to-unsigned-like(div-ceil(ranges::sizedistance(base_), static_cast<decltype(ranges::size(base_))>(n_));