Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use _Invoke_result_t<_Urng&> instead of _Urng::result_type in _Rng_from_urng #3002

Merged
merged 4 commits into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions stl/inc/xutility
Original file line number Diff line number Diff line change
Expand Up @@ -5705,12 +5705,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&>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't _Invoke_result_t<_Urng&> a bit heavyweight when we could simply decltype(_STD declval<_Urng&>()())?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but I don't think that this will ever be frequently instantiated in a program - so I'm not concerned about throughput here.


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 @@ -5754,7 +5754,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