]> git.ipfire.org Git - thirdparty/gcc.git/commit
libstdc++: Do not assume URBG::result_type exists [PR121919]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 30 Apr 2026 12:27:48 +0000 (13:27 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Fri, 1 May 2026 11:18:56 +0000 (12:18 +0100)
commit0a2b9dc9655f12e43a0e67f26ea21ff4f4e038fe
tree7d01f6bc4955dea02e28e50c2dfdce14e5d1418d
parentc1ac0abefe574cda048760b4540bb12097fe0876
libstdc++: Do not assume URBG::result_type exists [PR121919]

The ranges::sample and ranges::shuffle algorithms are supposed to work
with types which model std::uniform_random_bit_generator, which means
they should not assume that G::result_type is present. That isn't needed
to satisfy the concept. Change the algorithms to use decltype(__g())
instead of using result_type.

This isn't sufficient to fix the bug though, because those algorithms
use std::uniform_int_distribution and that class template's operator()
overloads depend on the more restrictive uniform random bit generator
requirements, which do include the presence of a nested result_type
member.

We need to change std::uniform_int_distribution to also use decltype
instead of the nested result_type, even though the standard says that
std::uniform_int_distribution is allowed to assume that result_type
exists.

There's yet another problem, which is that a type that returns random
bool values can model the concept, but doesn't meet the named
requirements and can't be used with std::uniform_int_distribution. That
isn't addressed by this change.

libstdc++-v3/ChangeLog:

PR libstdc++/121919
* include/bits/ranges_algo.h (__sample_fn, __shuffle_fn): Use
decltype(__g()) instead of remove_reference_t<_G>::result_type.
* include/bits/uniform_int_dist.h
(uniform_int_distribution::operator()): Use decltype(__urng())
instead of _UniformRandomBitGenerator::result_type
(uniform_int_distribution::__generate_impl): Likewise.
* testsuite/25_algorithms/sample/121919.cc: New test.
* testsuite/25_algorithms/shuffle/121919.cc: New test.

Reviewed-by: Nathan Myers <nmyers@redhat.com>
libstdc++-v3/include/bits/ranges_algo.h
libstdc++-v3/include/bits/uniform_int_dist.h
libstdc++-v3/testsuite/25_algorithms/sample/121919.cc [new file with mode: 0644]
libstdc++-v3/testsuite/25_algorithms/shuffle/121919.cc [new file with mode: 0644]