I'm working on generic class that will be used to run functions of different prototype for performance testing of algorithms.
I stuck because std::function
can't execute what it was bound with, here is sample code, with comment where the error happens:
#include <utility>
#include <functional>
template<typename ReturnType>
class Performance
{
public:
template<typename... Args>
using Algorithm = std::function<ReturnType(Args...)>;
template<typename... Params>
void run(const Algorithm<Params...>& ref, const Algorithm<Params...>& target)
{
// ERROR: term does not evaluate as taking 0 args
ref();
target();
}
};
void bar1(int, int)
{
// algorithm 1
}
void bar2(int, int)
{
// algorithm 2
}
int main()
{
using test = Performance<void>;
using Algorithm = test::Algorithm<int, int>;
int x = 0;
int y = 1;
Algorithm ref = std::bind(bar1, std::ref(x), std::ref(y));
Algorithm target = std::bind(bar2, std::ref(x), std::ref(y));
test foobar;
foobar.run(ref, target);
}
The problem is, the std::function
type, i.e. Algorithm
is declared to take two parameters (with type int
); when calling on them two arguments are required.
After std::bind
applied, the returned functors take no parameters; arguments (std::ref(x)
and std::ref(y)
) have been bound. Algorithm
should be declared as
using Algorithm = test::Algorithm<>;