public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict
@ 2020-08-21  6:19 TonyELewis at hotmail dot com
  2020-08-21  8:52 ` [Bug libstdc++/96731] " redi at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: TonyELewis at hotmail dot com @ 2020-08-21  6:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96731
           Summary: uniform_int_distribution requirement that its type
                    is_integral is too strict
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: TonyELewis at hotmail dot com
  Target Milestone: ---

Following on from https://github.com/ericniebler/range-v3/issues/1532 ...

I think the static_assert requirement in libstdc++'s uniform_int_distribution
code that its template argument type is_integral may be too strict.

I can't see any obvious mention of constraints on uniform_­int_­distribution's
IntType in http://eel.is/c++draft/rand.dist.uni.int

The motivating case is attempting to sample from a range-v3 view::indices. The
range-v3 clever tricks lead to uniform_int_distribution being invoked with a
range-v3 type.

This all works OK under libc++ because it doesn't impose any requirements on
the type. But the aforementioned static_assert in libstdc++ won't allow it.

The problem can be seen with the first error under GCC or Clang with -std=c++17
on this code:


#include <algorithm>
#include <iterator>
#include <random>
#include <vector>

#include <range/v3/view/indices.hpp>

void fill_vec_with_random_sample_of_first_n_ints( const size_t         
&prm_num_possible_vals,
                                                  std::vector<size_t>
&prm_result,
                                                  std::mt19937        &prm_rng
                                                  ) {
        auto some_indices = ranges::views::indices( prm_num_possible_vals );
        std::sample(
                std::begin( some_indices ),
                std::end  ( some_indices ),
                std::begin( prm_result ),
                static_cast<ptrdiff_t>( prm_result.size() ),
                prm_rng
        );
}


Compiler Explorer : https://godbolt.org/z/MxrazP


One option is that libstdc++ could use std::numeric_limits<T>::is_integer
instead of std::is_integral, so that range-v3 could then specialise accordingly
for its type.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/96731] uniform_int_distribution requirement that its type is_integral is too strict
  2020-08-21  6:19 [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict TonyELewis at hotmail dot com
@ 2020-08-21  8:52 ` redi at gcc dot gnu.org
  2020-08-21  8:55 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-21  8:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Tony E Lewis from comment #0)
> I can't see any obvious mention of constraints on
> uniform_­int_­distribution's IntType in
> http://eel.is/c++draft/rand.dist.uni.int

See http://eel.is/c++draft/rand#req.genl-1.5

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/96731] uniform_int_distribution requirement that its type is_integral is too strict
  2020-08-21  6:19 [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict TonyELewis at hotmail dot com
  2020-08-21  8:52 ` [Bug libstdc++/96731] " redi at gcc dot gnu.org
@ 2020-08-21  8:55 ` redi at gcc dot gnu.org
  2020-08-24  6:44 ` TonyELewis at hotmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-21  8:55 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2020-08-21

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Since it's undefined, we can do whatever we want, including causing compilation
to fail with a static_assert, or ignoring it (as libc++ seems to do), or using
another condition to check for sufficiently integer-like types (possibly only
in non-strict modes).

Confirming as an enhancement request.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/96731] uniform_int_distribution requirement that its type is_integral is too strict
  2020-08-21  6:19 [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict TonyELewis at hotmail dot com
  2020-08-21  8:52 ` [Bug libstdc++/96731] " redi at gcc dot gnu.org
  2020-08-21  8:55 ` redi at gcc dot gnu.org
@ 2020-08-24  6:44 ` TonyELewis at hotmail dot com
  2020-08-26 19:15 ` redi at gcc dot gnu.org
  2020-09-04 10:39 ` TonyELewis at hotmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: TonyELewis at hotmail dot com @ 2020-08-24  6:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Tony E Lewis <TonyELewis at hotmail dot com> ---
Thanks for correcting my failure to find the relevant part of the standard and
for confirming this as an enhancement request.

Yes please. If libstdc++'s sample() could play nicely with range-v3's
view::indices, that'd be much appreciated.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/96731] uniform_int_distribution requirement that its type is_integral is too strict
  2020-08-21  6:19 [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict TonyELewis at hotmail dot com
                   ` (2 preceding siblings ...)
  2020-08-24  6:44 ` TonyELewis at hotmail dot com
@ 2020-08-26 19:15 ` redi at gcc dot gnu.org
  2020-09-04 10:39 ` TonyELewis at hotmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: redi at gcc dot gnu.org @ 2020-08-26 19:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Created attachment 49135
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49135&action=edit
Patch to relax std::uniform_int_distribution requirements

This patch allows a custom integer-like type to be used with
std::uniform_int_distribution, but it's harder than it seems.

We need to use std::make_unsigned, which is undefined for non-integral types.
The patch will use _IntType is numeric_limits<_IntType>::is_signed is false,
otherwise it will use make_unsigned_t<_IntType> if possible, and unsigned long
long otherwise.

But the example still fails, because std::sample also requires an integral
type, and also wants to use make_unsigned. And the standard is clear that it's
not just undefined, but ill-formed and requires a diagnostic:

> Mandates: For the overload in namespace std, Distance is an integer type

I don't plan to work on this any further for now.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug libstdc++/96731] uniform_int_distribution requirement that its type is_integral is too strict
  2020-08-21  6:19 [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict TonyELewis at hotmail dot com
                   ` (3 preceding siblings ...)
  2020-08-26 19:15 ` redi at gcc dot gnu.org
@ 2020-09-04 10:39 ` TonyELewis at hotmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: TonyELewis at hotmail dot com @ 2020-09-04 10:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Tony E Lewis <TonyELewis at hotmail dot com> ---
Thanks very much for your work on this.

That's a shame but I appreciate the problems you've highlighted.

> I don't plan to work on this any further for now.

Yes, fair enough.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2020-09-04 10:39 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-21  6:19 [Bug libstdc++/96731] New: uniform_int_distribution requirement that its type is_integral is too strict TonyELewis at hotmail dot com
2020-08-21  8:52 ` [Bug libstdc++/96731] " redi at gcc dot gnu.org
2020-08-21  8:55 ` redi at gcc dot gnu.org
2020-08-24  6:44 ` TonyELewis at hotmail dot com
2020-08-26 19:15 ` redi at gcc dot gnu.org
2020-09-04 10:39 ` TonyELewis at hotmail dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).