Title
chunk_view::size should preserve the signedness of the size of the underlying range
Status
new
Section
[range.chunk.view.input][range.chunk.view.fwd]
Submitter
Hewill Kang

Created on 2022-07-15.00:00:00 last changed 4 weeks ago

Messages

Date: 2022-07-17.10:48:32

Proposed resolution:

This wording is relative to N4910.

  1. Modify [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 [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_));
    

Date: 2022-07-17.12:26:30

Currently, the Effects of chunk_view::size simply returns to-unsigned-like(div-ceil(ranges::distance(base_), n_)), where div-ceil is defined in [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.

History
Date User Action Args
2022-07-16 17:46:58adminsetmessages: + msg12594
2022-07-15 00:00:00admincreate