Skip to content

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

Merged
Stephan T. Lavavej (StephanTLavavej) merged 4 commits into
microsoft:mainfrom
cpplearner:invoke_result
Aug 9, 2022
Merged

Stephan T. Lavavej (StephanTLavavej) merged 4 commits into
microsoft:mainfrom
cpplearner:invoke_result

Conversation

@cpplearner

Copy link
Copy Markdown
Contributor

C++20 uniform_random_bit_generator does not require the presence of member result_type.

This is tested in libc++ tests std/algorithms/alg.modifying.operations/alg.random.shuffle/ranges_shuffle.pass.cpp and std/algorithms/alg.modifying.operations/alg.random.sample/ranges_sample.pass.cpp. Unfortunately they fail even after this change, because they assume that std::array iterators are pointers.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) added the bug Something isn't working label Aug 7, 2022
Comment thread stl/inc/xutility Outdated
@StephanTLavavej

Copy link
Copy Markdown
Member

Thanks for finding and fixing this bug! I've pushed a fix for value categories with a corresponding test change (fails without the fix, passes with it).

@MattStephanson

Copy link
Copy Markdown
Contributor

C++20 uniform_random_bit_generator does not require the presence of member result_type

That's true for the concept, but doesn't [rand.req.urng]/3 require "G provides a nested typedef-name result_­type that denotes the same type as invoke_­result_­t<G&>"? These generators in libc++ meet the URBG concept, but not the named requirements.

@cpplearner

Copy link
Copy Markdown
Contributor Author

C++20 uniform_random_bit_generator does not require the presence of member result_type

That's true for the concept, but doesn't [rand.req.urng]/3 require "G provides a nested typedef-name result_­type that denotes the same type as invoke_­result_­t<G&>"? These generators in libc++ meet the URBG concept, but not the named requirements.

Old std::shuffle requires the generator type to meet the uniform random bit generator named requirements (and thus requires member result_type), but C++20 ranges::shuffle only requires the type to model the concept.

Since ranges::shuffle uses _Rng_from_urng, the latter must not rely on result_type.

If we want to be strict, we could change std::shuffle to check the presence of result_type.

@MattStephanson

Copy link
Copy Markdown
Contributor

Comment thread stl/inc/xutility
public:
using _Ty0 = make_unsigned_t<_Diff>;
using _Ty1 = typename _Urng::result_type;
using _Ty1 = _Invoke_result_t<_Urng&>;

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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.

@StephanTLavavej

Copy link
Copy Markdown
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) changed the title Use _Invoke_result_t<_Urng> instead of _Urng::result_type in _Rng_from_urng Use _Invoke_result_t<_Urng&> instead of _Urng::result_type in _Rng_from_urng Aug 9, 2022
@StephanTLavavej
Stephan T. Lavavej (StephanTLavavej) merged commit ced5cde into microsoft:main Aug 9, 2022
@StephanTLavavej

Copy link
Copy Markdown
Member

Thanks for randomly fixing this bug! 🐞 🛠️ 😹

Igor Zhukov (fsb4000) pushed a commit to fsb4000/STL that referenced this pull request Aug 13, 2022
…ng_from_urng` (microsoft#3002)

Co-authored-by: Stephan T. Lavavej <stl@nuwen.net>
@cpplearner
S. B. Tam (cpplearner) deleted the invoke_result branch August 25, 2022 06:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants