mercredi 30 mars 2016

Why would the param_type constructor be explicit for a random distribution?

I'm trying to compile this program (see it live here):

int main() {
  std::random_device engine;
  std::uniform_int_distribution<size_t> dis;
  std::cout << dis(engine, {0, 5}) << std::endl;
}

But it fails with the error message:

error: converting to 'const std::uniform_int_distribution<long unsigned int>::param_type' from initializer list would use explicit constructor 'std::uniform_int_distribution<_IntType>::param_type::param_type(_IntType, _IntType) [with _IntType = long unsigned int]'
   std::cout << dis(engine, {0, 5}) << std::endl;

Obviously, it is the explicit constructor of param_type that prevents us from doing this. But why specifying it as explicit in the first place? It's verbose and silly if one has to write

std::cout << dis(engine, decltype(dis)::param_type(0, 5)) << std::endl;

Any explanations on this? And also, how could I achieve what I want in a succinct and elegant way, given that the param_type constructor is explicit in the standard? Note that, in practice, the range may be different each time I invoke dis. So, supplying a range at the construction time of dis does not help.

Aucun commentaire:

Enregistrer un commentaire