I have a std::vector
of struct
s that contain several fields, like the following:
struct stats
{
double mean;
double median;
double rms;
};
std::vector<stats> data;
I'd like to design a function that operates on the vector
, for example, builds a histogram. I'd like to be able to specify what filed of the struct
s should this function operate on. For example:
build_histogram(data, get_mean);
build_histogram(data, get_median);
build_histogram(data, get_rms);
I tried to implement some getters in the stats
class, like this:
struct stats
{
double mean;
double median;
double rms;
struct get_mean { double operator() () { return mean; };
struct get_median { double operator() () { return median; };
struct get_rms { double operator() () { return rms; };
};
But it says that's an invalid use of non-static members mean
, median
and rms
.
How could I implement it correctly?