STL icon indicating copy to clipboard operation
STL copied to clipboard

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

Open foonathan opened this issue 2 years ago • 2 comments

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

foonathan avatar Apr 19 '23 12:04 foonathan

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.

frederick-vs-ja avatar Apr 19 '23 17:04 frederick-vs-ja

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

hewillk avatar Oct 22 '23 08:10 hewillk