只绑定一个参数,稍后绑定其余参数

Bind just one argument, later the rest

本文关键字:参数 绑定 余参数 一个      更新时间:2023-10-16

我想测试不同的算法来计算加速(单核、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将返回一个函数对象,该对象调用第一个参数设置为1fraktale_cpu_multi()函数,并将其三个参数作为第二、第三和第四个参数转发给fraktale_cpu_multi()

您需要这样的东西:

#include <functional>
std::bind(fraktale_cpu_multi, 1,
          std::placeholders::_1, std::placeholders::_2, std::placeholders::_3)