`<algorithm>`: Audit divergence of difference types between wrapped and unwrapped iterators and correct the uses
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 11.2k
- Forks
- 1.7k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 22
Description
Describe the bug
Currently, this program, when compiled with MSVC using -std:c++latest -W3, triggers warning C4244.
#include <algorithm>
#include <iterator>
#include <vector>
template <class T>
struct small_ator {
using value_type = T;
using size_type = unsigned short; // !!!
using difference_type = short; // !!!
small_ator() = default;
template <class U>
constexpr small_ator(const small_ator<U>&) noexcept {}
T* allocate(size_type n) {
return std::allocator<T>{}.allocate(n);
}
void deallocate(T* p, size_type n) {
return std::allocator<T>{}.deallocate(p, n);
}
friend bool operator==(const small_ator&, const small_ator&) = default;
template <class U>
friend constexpr bool operator==(const small_ator&, const small_ator<U>&) {
return true;
}
};
int main() {
std::vector<int, small_ator<int>> v{1, 2, 3, 4};
std::inplace_merge(v.begin(), std::next(v.begin(), 2), v.end());
}
There're probably other algorithms incorrectly triggering this warning.
The cause seems to be that MSVC STL generally assumes that a wrapped iterator type has the same difference type as its corresponding unwrapped one. The assumption is incorrect when the wrapped iterator is an iterator type of basic_string or vector or some iterator adapting it. In such a case, the difference type of the wrapped iterator is determined by the iterator, while the difference type of the unwrapped iterator is ptrdiff_t.
Command-line test case
https://godbolt.org/z/c99638j69
Expected behavior
No warning C4244 due to small difference_type/size_type of allocator types, for all algorithms.
STL version
Probably all existing versions where this program is accepted.
Additional context
Add any other context about the problem here.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the provided MSVC command-line example and the std::inplace_merge call that triggers C4244. Audit the algorithms for mismatched difference types between wrapped and unwrapped iterators, especially when vector or basic_string iterators are involved. Done means the repro and equivalent cases no longer produce the warning across the affected algorithms.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100