microsoft / microsoft/STL

`<algorithm>`: `ranges::reverse_copy` and `ranges::search` missing `difference_type` casting for `contiguous_iterator` operations

Open
#2,892 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug ranges
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

https://github.com/microsoft/STL/blob/04ee87830a404437f1691d6a3e8809e4986d8493/stl/inc/algorithm#L4479

The lhs type is _Out, while the rhs is iter_difference_t<_It>.

https://github.com/microsoft/STL/blob/ef62d3fa0b8e4e2406b9bb74e916e1ca8a1df802/stl/inc/xutility#L5489

The same goes for ranges::search.

#include <algorithm>

int* p;

struct O {
  using iterator_category = std::contiguous_iterator_tag;
  using difference_type = int;
  using element_type = int;
  element_type& operator*() const;
  O& operator++();
  O operator++(int);
  O& operator--();
  O operator--(int);
  O& operator+=(difference_type);
  O& operator-=(difference_type);
  element_type* operator->() const;
  element_type& operator[](difference_type) const;
  friend O operator+(O, difference_type);
  friend O operator+(difference_type, O);
  friend O operator-(O, difference_type);
  friend difference_type operator-(O, O);
  auto operator<=>(const O&) const = default;

  void operator+=(decltype(p - p)) = delete;
};

int main() {
  std::ranges::reverse_copy(p, p, O{});
  std::ranges::search(O{}, O{}, p, p);
}

https://godbolt.org/z/osEE96695

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read the linked operations in stl/inc/algorithm and stl/inc/xutility, focusing on the contiguous_iterator paths for ranges::reverse_copy and ranges::search. Compile the provided reproducer to observe the type mismatch, then check the relevant algorithm tests and add coverage showing both calls work with the custom iterator. Done means the reproducer compiles and the regression tests pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.