microsoft / microsoft/STL

`<algorithm>`: Should `_Rng_from_urng` use `make-unsigned-like-t`?

Open
#2,884 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

#include <vector>
#include <random>
#include <ranges>
#include <algorithm>

int main() {
  std::vector<std::size_t> v(10);
  std::ranges::sample(
    std::views::iota(0ULL, 42ULL),
    v.begin(), 3, std::mt19937{std::random_device{}()});
}

https://godbolt.org/z/bMredxf58

In the above example, the difference_type of iota_view is _Signed128, which makes the instantiation of _Rng_from_urng fail since it uses make_unsigned_t which cannot work with integer-class types.

https://github.com/microsoft/STL/blob/ef62d3fa0b8e4e2406b9bb74e916e1ca8a1df802/stl/inc/xutility#L5696-L5700

It's worth noting that gcc will only work with -std=gnu++20 because __int128 is an integer type only in gnu-mode. gcc will also fail the static_assert under -std=c++20, but this assertion comes from the implementation of std::sample, so there is no reference value because std::sample is not for C++20 iterators system.

In [alg.random.sample], I don't see any requirement that the above code violates, so should I consider it a bug?

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 by reproducing the sample with the linked Godbolt code, then inspect stl/inc/xutility around lines 5696-5700 and compare the behavior with [alg.random.sample]. Determine whether the standard permits this integer-class difference_type and whether the make_unsigned_t usage is appropriate; done means a resolved conformance decision with corresponding implementation or regression-test changes if needed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.