Skip to content

Commit

Permalink
Use _Invoke_result_t<_Urng&> instead of _Urng::result_type in `_R…
Browse files Browse the repository at this point in the history
…ng_from_urng` (#3002)

Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
cpplearner and StephanTLavavej authored Aug 9, 2022
1 parent b1c4c50 commit ced5cde
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
6 changes: 3 additions & 3 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -5847,12 +5847,12 @@ 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;
using _Ty1 = _Invoke_result_t<_Urng&>;

using _Udiff = conditional_t<sizeof(_Ty1) < sizeof(_Ty0), _Ty0, _Ty1>;

explicit _Rng_from_urng(_Urng& _Func) : _Ref(_Func), _Bits(CHAR_BIT * sizeof(_Udiff)), _Bmask(_Udiff(-1)) {
for (; (_Urng::max)() - (_Urng::min)() < _Bmask; _Bmask >>= 1) {
for (; static_cast<_Udiff>((_Urng::max)() - (_Urng::min)()) < _Bmask; _Bmask >>= 1) {
--_Bits;
}
}
Expand Down Expand Up @@ -5896,7 +5896,7 @@ public:
private:
_Udiff _Get_bits() { // return a random value within [0, _Bmask]
for (;;) { // repeat until random value is in range
_Udiff _Val = _Ref() - (_Urng::min)();
_Udiff _Val = static_cast<_Udiff>(_Ref() - (_Urng::min)());

if (_Val <= _Bmask) {
return _Val;
Expand Down
19 changes: 19 additions & 0 deletions tests/std/tests/P0896R4_ranges_alg_shuffle/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,25 @@ struct instantiator {
}
};

void test_urbg() { // COMPILE-ONLY
struct RandGen {
static constexpr bool min() {
return false;
}
static constexpr bool max() {
return true;
}
bool operator()() & {
return false;
}
};

STATIC_ASSERT(uniform_random_bit_generator<RandGen>);

int arr[1] = {};
ranges::shuffle(arr, RandGen{});
}

int main() {
printf("Using seed: %u\n", seed);

Expand Down

0 comments on commit ced5cde

Please sign in to comment.