lundi 1 juin 2015

pack a random number generator

The C++11 std library has several random number generators (RNG), each implementing the concept UniformRandomNumberGenerator. These can then be used as argument for random distributions, see also this documentation for an overview.

The advantage of this design is that the choice of the underlying RNG engine is de-coupled from its application. However, the design also requires all calls to the RNG to be fully inlined (if the RNG type is to remain unspecified as template parameter). Thus, in

struct complicated_random_distribution
{
  /*
     some data and auxiliary methods here
  */
  // complicated; may call gen::operator() many times
  template<typename RNG>
  some_type operator()(RNG&gen) const;
};

the member complicated_random_distribution::operator() cannot be straightforwardly implemented out of line.

For such a out-of-line implementation, one ideally would want some way to pack a RNG in the same way as std::function<> packs any callable object. (simply using std::function and providing the values for RNG::min() and RNG::max() as arguments to an out-of-line function is restrictive and will not allow to use, say, std::uniform_real_distribution<> inside).

Hence, one would want a way to pack a RNG (satisfying said concept) in a way similar to std::function. How can this be done? Are implementations for this available? Will the std library provide this in the future? Or am I after a red herring?

Aucun commentaire:

Enregistrer un commentaire