ericniebler / stl2

LaTeX and Markdown source for the Ranges TS/STL2 and associated proposals
88 stars 8 forks source link

iota_view::iterator::operator- doesn't handle unsigned ints very well. #616

Closed ericniebler closed 5 years ago

ericniebler commented 5 years ago

Once we fix #614, we have an additional problem in iota_view<U,U>::iterator::operator- for an unsigned integral type U. The expected behavior is:

return x.value_ - y.value_;

However, if y.value_ > x.value_, then the result wraps to a large positive number. Once #614 is fixed, operator- will return a type that is capable of representing this large number, and so rather than returning a negative, we'll return a large positive.

Assuming 2s compliment (can we yet?), I think we can instead do the following for integral types:

using D1 = make_signed_t<W>;
using D2 = iter_difference_t<W>;  // Once #614 is fixed, this has more bits for unsigned ints
return (D2) (D1) (i1 - i0);
ericniebler commented 5 years ago

Fixed by P0522.