只绑定一个参数,稍后绑定其余参数
Bind just one argument, later the rest
我想测试不同的算法来计算加速(单核、cuda、多核)。函数标题如下:
void fraktale_cpu_single(pfc::bitmap * bmp,
pfc::RGB_3_t * color_table,
const par::complex<float> C)
我总是要初始化相同的数据,所以我想写一个函数,它将调用函数指针。
void do_function_with_pic(
std::function<void(pfc::bitmap * bmp,
pfc::RGB_3_t * color_table,
const par::complex<float> C)> Func,
const string pic_name)
在单核和cuda中没有问题,在多核中,我希望能够改变处理这个问题的线程数量,所以我的多核函数还有一个参数:
void fraktale_cpu_multi(size_t threads,
pfc::bitmap * bmp,
pfc::RGB_3_t * color_table,
const par::complex<float> C)
我试过这个:
do_function_with_pic(bind(fraktale_cpu_multi, 1), "cpu_multi.bmp");
但我犯了一个错误,因为其他参数都没有设置,我该怎么办?-Boost lib也可用!
您必须为函数的剩余参数使用占位符:
#include <functional>
using std::bind;
using namespace std::placeholders; // Namespace for _1, _2, ...
do_function_with_pic(bind(fraktale_cpu_multi, 1, _1, _2, _3), "cpu_multi.bmp");
std::bind
将返回一个函数对象,该对象调用第一个参数设置为1
的fraktale_cpu_multi()
函数,并将其三个参数作为第二、第三和第四个参数转发给fraktale_cpu_multi()
。
您需要这样的东西:
#include <functional>
std::bind(fraktale_cpu_multi, 1,
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)
相关文章:
- 为什么 std::绑定错误参数可以成功?
- C++初始化 std::function 时如何将占位符绑定到引用/引用参数?
- 绑定到可变参数成员函数
- 如何在 C# 中将带有参数的函数绑定到包中
- 如何将参数绑定到构造函数?
- C++ 如何将参数绑定到具有相同返回类型的函数,并将它们全部存储在一个容器中
- 函数参数绑定通过参考与传递指针传递数组的规则
- C 如何将参数绑定到接受函数的函数
- 模板参数绑定
- 是由BOOST ::返回的函子,仅使用绑定的参数绑定,等效于不采用参数的函数
- 当常量引用参数绑定到右值时,右值是否保持其"status"?
- 用emscripten中的参考参数绑定函数
- 如何将Boost ::函数对象与所有参数绑定
- std ::将参数绑定到没有对象的情况下的成员函数
- 隐马尔可夫模型-在C++中实现参数绑定
- 函数参数绑定的完美转发和歧义
- 为容器中的每个对象调用具有一个参数(绑定)的成员函数
- std::bind():将lambda与右值引用作为参数绑定
- 与派生函数参数绑定
- 与模板模板类一起使用的自定义模板参数绑定器