C++ Logo

std-proposals

Advanced search

[std-proposals] Requirements on integer types with <random> don't make sense with predefined engines

From: Halalaluyafail3 <luigighiron_at_[hidden]>
Date: Wed, 6 Nov 2024 02:52:02 -0500
N4993 Section 29.5.3.1 [rand.req.genl] Paragraph 1:
> Throughout this subclause 29.5, the effect of instantiating a template:
>
> ...
>
> - that has a template type parameter named IntType is undefined unless the
> corresponding template argument is cv-unqualified and is one of short, int,
> long, long long, unsigned short, unsigned int, unsigned long, or unsigned
> long long.
>
> - that has a template type parameter named UIntType is undefined unless the
> corresponding template argument is cv-unqualified and is one of unsigned
> short, unsigned int, unsigned long, or unsigned long long.

This section requires that only standard integer types which are not ordinary
character types are valid to be used with the standard engines.

N4993 Section 29.5.6 [rand.predef] Before Paragraph 3:
> using mt19937 =
> mersenne_twister_engine<uint_fast32_t, 32, 624, 397, 31, 0x9908'b0df,
> 11, 0xffff'ffff, 7, 0x9d2c'5680, 15, 0xefc6'0000, 18, 1'812'433'253>;

N4993 Section 29.5.2 [rand.synopsis]:
> template<class UIntType, size_t w, size_t n, size_t m, size_t r,
> UIntType a, size_t u, UIntType d, size_t s,
> UIntType b, size_t t,
> UIntType c, size_t l, UIntType f>
> class mersenne_twister_engine;

The standard type alias std::mt19937 instantiates std::mersenne_twister_engine
with the type std::uint_fast32_t for UIntType. However, there is no requirement
that std::uint_fast32_t has to be one of those types. What should happen with an
implementation that does not define std::uint_fast32_t to be one of those types
when using std::mt19937 (e.g. defining it as unsigned char or a type wider than
long long)? After discussion on discord I think there are 3 possible
interpretations of what should happen here:
1. On such an implementation, std::mt19937 is not valid to use.
2. On such an implementation, std::uint_fast32_t must be valid for UIntType;
   i.e. it can be used anyways despite not being one of the listed types.
3. Such an implementation cannot be a conforming hosted implementation,
   this is effectively a requirement on what std::uint_fast32_t can be.
In N4993 Section 29.5.6 [rand.predef] and N4993 Section 29.5.4.4 [rand.eng.sub]
Paragraph 7 there are several other uses of type aliases from cstdint as
template type arguments for parameters named UIntType (and thus have the same
problems).

I'm thinking about making a LWG issue for this, though it seems like it is
preferred to post to this mailing list first.

Received on 2024-11-06 07:55:20