I am trying to generalize my benchmarking function, by having it receive the function to benchmark as the first parameter and the number of iterations as the second.
But since the function to benchmark needs to receive additional parameters, I thought I would fill in the parameters in the body of a lambda function and pass that to the benchmarking function. (I think that is called currying?)
Anyway I can not get it to compile:
main.cpp:43:62: error: invalid initialization of non-const reference of type ‘std::function<double*()>&’ from an rvalue of type ‘main(int, char**)::<lambda()>’ bench::bench([=](){rng::gpu_r_exp((int) 10e6, lambda);}, 50); The function declaration looks like this:
void bench(std::function<double*()>& funct_to_bench, int repeats); and I use it like this:
bench::bench([=](){rng::gpu_r_exp((int) 10e6, lambda);}, 50); Since the compiler bickers about non-const again, I should maybe add, that gpu_r_exp utilizes a global variable which stores the rngState (it also did not like non-const parameters in gpu_r_exp).
I am really stuck. I just want to fill in the parameters and pass the pre-prepared function handle to the benchmarking function, so that it can wrap a timer with a progress bar around it.
EDIT: I should add that the parameter called lambda is a double, which is the parameter of the exponential distribution and has nothing to do with the lambda function.
std::function. You need to pass astd::functionto the function since it requires a non-const lvalue reference.std::function &, that's not the same as astd::function. non-const lvalue references (whichstd::function &is) do not allow for conversions. You have to give it exactly what it is.void foo(int&), theint&is a non-const lvalue refernce. Invoid foo(const int &)theconst int &is a const lvalue reference. Invoid foo(int&&)theint&&is a rvalue reference. Invoid foo(const int&&)theconsat int&&is a const rvalue reference.std::functionit wont work. A const-lvalue reference does allow for conversions and will let the code work.