microsoft / microsoft/STL

`<ranges>`: `take_view`/`drop_view`/`slide_view`'s `range_difference_t<const V>` issue

Open
#2,894 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

https://github.com/microsoft/STL/blob/04ee87830a404437f1691d6a3e8809e4986d8493/stl/inc/ranges#L2831-L2836

Since range_difference_t<V> is not guaranteed to be the same as range_difference_t<const V>, we'd better do type casting before calling std::min. The standard has no such problem because it always calls ranges​::​next(ranges​::​begin(base_), count_, ranges​::​end(base_)), and range_difference_t<const V> is guaranteed to be implicitly converted to range_difference_t<V> because they are both signed.

#include <ranges>

auto s = std::views::single(0ULL);
auto o = std::views::iota(0ULL, 1ULL);

struct R {
  auto begin() { return s.begin(); }
  auto end() { return s.end(); }
  auto begin() const { return o.begin(); }
  auto end() const { return o.end(); }
};

int main() {
  const auto d = R{} | std::views::drop(1);
  auto b = d.begin();
}

https://godbolt.org/z/Pj5cP394n

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

Start at stl/inc/ranges around lines 2831-2836 and reproduce the reported const and non-const range difference types with the supplied example or Godbolt link. Check the affected take_view, drop_view, and slide_view paths, then add or update coverage so the example compiles without the reported type mismatch.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.