【发布时间】:2020-05-23 05:18:34
【问题描述】:
我正在研究通用类,该类将用于运行不同原型的函数以进行算法性能测试。
我卡住了,因为std::function 无法执行它所绑定的内容,这里是示例代码,并在错误发生的地方附上注释:
#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);
}
【问题讨论】:
-
两者都应该占用两个
ints,不是吗? -
是的,他们已经与
ints 和std::bind绑定在一起,在我看来std::bind在这里什么也没做,有没有其他解决方案?
标签: c++ c++11 templates std-function stdbind