std::bind 和 std::函数术语不值为接受 0 个参数?
std::bind and std::function term does not evalue as taking 0 arguments?
我正在研究通用类,该类将用于运行不同原型的函数以进行算法的性能测试。
我卡住了,因为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<>;
住
相关文章:
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- 为什么 std::function 可以作为 std::not2 的参数?
- 传递给std::function template的template参数究竟代表什么
- 转换函数,将 std::数组的双精度作为参数或双精度作为参数单独转换
- 在C++中,使用带有 std::optional 参数的函数<T>来表示可选参数是否有意义?
- 将函数参数"const char*"转换为"std::string_view"是
- 当指向对象的指针作为参数传递给 std::thread 时,内存可见性
- 为什么 std::绑定错误参数可以成功?
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- std::vector 没有重载函数的实例与参数列表匹配
- std::span<const T> 作为函数模板中的参数
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 如果模板没有可变参数,则 Lambda 被推导出为 std::function
- 将参数打包的参数传递到 std::queue 中,以便稍后使用不同的函数调用
- 在构造函数中使用可变参数初始化 std::tuple
- 为什么我不能将引用作为 std::async 的函数参数传递
- 转发变量参数列表以模拟 std::thread
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 可变参数模板参数扩展 类型为 std::function 的类成员