microsoft / STL

MSVC's implementation of the C++ Standard Library.
Other
10.04k stars 1.47k forks source link

`<algorithm>`/`<iterator>`: contiguous iterators with non-`ptrdiff_t` difference types #3663

Closed foonathan closed 1 week ago

foonathan commented 1 year ago

Describe the bug

We're having a contiguous iterator whose difference_type is int. STL algorithms then translate it to pointers and back, but doing so they e.g. call the iterator's operator- with a std::ptrdiff_t, not difference_type, like here: https://github.com/microsoft/STL/blob/3c1f3d79ea8db9f20ef0854704e3eb131972a606/stl/inc/xutility#L4944

This causes a truncation warning.

Command-line test case

https://godbolt.org/z/YbojbWPMd

Expected behavior

No warning should happen, the implementation should cast to the appropriate difference type before invoking the operator.

STL version

Microsoft Visual Studio Professional 2022
Version 17.1.0
frederick-vs-ja commented 1 year ago

We have a number of issues on difference types. I guess there're also remain issues in <vector> ~, <deque>,~ and <xstring>. CC @hewillk.

Edit: deque is likely to be fixed in #4049.

hewillk commented 10 months ago

The issue occurs with _Copy_memmove too:

https://github.com/microsoft/STL/blob/3c1f3d79ea8db9f20ef0854704e3eb131972a606/stl/inc/xutility#L4636

Although I think the contiguous_iterator's difference type should at least have a width greater than or equal to ptrdiff_t. testcase: https://godbolt.org/z/Tqhbs7j9v