https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121919

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-16 branch has been updated by Jonathan Wakely
<[email protected]>:

https://gcc.gnu.org/g:0a19c6cbabd60737967df931f9455c800630faff

commit r16-8867-g0a19c6cbabd60737967df931f9455c800630faff
Author: Jonathan Wakely <[email protected]>
Date:   Thu Apr 30 13:27:48 2026 +0100

    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 <[email protected]>
    (cherry picked from commit 0a2b9dc9655f12e43a0e67f26ea21ff4f4e038fe)

Reply via email to