std::bind 和 std::函数术语不值为接受 0 个参数?

std::bind and std::function term does not evalue as taking 0 arguments?

本文关键字:std 参数 bind 函数 术语      更新时间:2023-10-16

我正在研究通用类,该类将用于运行不同原型的函数以进行算法的性能测试。

我卡住了,因为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);
}

问题是,std::function类型,即Algorithm声明为采用两个参数(类型为int(;调用它们时,需要两个参数。

应用std::bind后,返回的函子不带任何参数;参数(std::ref(x)std::ref(y)(已被绑定。Algorithm应声明为

using Algorithm = test::Algorithm<>;