I have a couple of boiler-plate functions I would like to replace with a template. They look roughly like:
std::vector<double> generate_means(
std::mt19937& g, unsigned int N,
double lower = -1.0, double upper = 1.0
) {
std::uniform_real_distribution<double> dist(lower, upper);
std::function<double()> rng = std::bind(dist, g);
std::vector<double> res(N);
std::generate(std::begin(res), std::end(res), gen);
return res;
}
The elements that need abstraction are return type (only contained type, always vector
is fine) the arguments after N
(e.g., lower
and upper
in this case) and the distribution (e.g., std::uniform_real_distribution
).
What I'd like roughly to be able write:
auto generate_means = generate_template<
double, // results vector<double>
std::uniform_real_distribution, // uses uniform distro
double=-1.0,double=1.0 // with default args
>
auto generate_norm_deviates = generate_template<
double, // still provides vector<double>
std::normal_distribution, // different distro
double=0, double=1.0 // different defaults
>
auto generate_category_ids = generate_template<
unsigned int,
std::uniform_int_distribution,
unsigned int=0, unsigned int // again with two args, but only one default
>
I have some sub-pieces
template <class NUMERIC>
using generator = std::function<NUMERIC()>;
template <class NUMERIC>
std::vector<NUMERIC> series(unsigned int length, generator<NUMERIC> gen) {
std::vector<NUMERIC> res(length);
std::generate(std::begin(res), std::end(res), gen);
return res;
};
but when I try assembling like, for example
template <class NUMERIC, class DIST, class...Args>
std::vector<NUMERIC> generator_template(
std::mt19937& g, unsigned int N,
Args... args
) {
DIST<NUMERIC> dist(&args...);
generator<NUMERIC> gen = std::bind(dist, g);
return series(N, gen);
}
I run into compile errors (in this case error: expected unqualified-id
). Is what I'd like approximately achievable? Is this approach in the right direction, or do I need do something fundamentally different? If it is in the right direction, what am I missing?
EDIT:
For application constraints: I'd like to be able to declare the generators with defaults for arguments, but I do need to occasionally use them without the defaults. Not having defaults is just inconvenient, however, not fatal. Example:
//... assorted calculations...
auto xmeans = generate_means(rng, 100); // x on (-1,1);
auto ymeans = generate_means(rng, 100); // y on (-1,1);
auto zmeans = generate_means(rng, 100, 0, 1); // z on (0,1);