Take the following C++11 (and C++14 wording) from the requirements for random number generation class templates:
[C++11: 26.5.1.1/1e]: 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.
Due to e.g.:
[C++11: 26.5.8.2.1/1]: [..]
template<class IntType = int>
class uniform_int_distribution
[..]
we can't legally do things like this:
#include <iostream>
#include <random>
#include <cstdint>
int main()
{
std::default_random_engine generator;
std::uniform_int_distribution<std::uint8_t> distribution(0, 100);
std::cout << +distribution(generator);
}
Here are some real-world results from my tests:
g++ (GCC) 5.0.0 20141119 (experimental) - compiles but outputs 0 in every run
g++ (GCC) 4.9.0 - compiles but outputs 0 in every run (
demo)
g++ (GCC) 4.8.1 - compiles but outputs 0 in every run (
demo)
MSVS 2012 - builds and runs and has intuitive output
MSVS 2013 - builds and runs and has intuitive output
MSVS 2015 - compilation error: "invalid template argument for uniform_int_distribution"
Due to the UB, all of these results are perfectly compliant. But can't we deem this to be a flaw in the standard itself? It seems that, in this day and age of standardised fixed-width types, allowing only the standard integer types ([basic.fundamental]/2) for IntType is a fundamental limitation, and an unnecessary one at that.
Cheers,
Tom