Skip to content

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

Description

@hewillk
#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.

STL/stl/inc/xutility

Lines 5696 to 5700 in ef62d3f

template <class _Diff, class _Urng>
class _Rng_from_urng { // wrap a URNG as an RNG
public:
using _Ty0 = make_unsigned_t<_Diff>;
using _Ty1 = typename _Urng::result_type;

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions