绑定参数多于所需的函数,并向其传递明确的参数

Binding function with more arguments than needed and passing definite arguments to it

本文关键字:参数 于所需 函数 绑定      更新时间:2023-10-16

让我有一个函数

void f1(type_a a, type_b b, type_c c)

我想把它转换成

void f2(type_a a, type_b b)

其中将传递我的对象,而不是c。

我如何做到这一点?

我认为是这样的

boost::bind(&f1, _1, _2, c_default_value);

C++不是一种函数式语言,因此不能对函数进行真正的部分应用。您可以做以下操作:

#include <boost/bind.hpp>
void f1(int a, double b, char c)
{}
int main()
{
  auto binder = boost::bind(&f1, _1, _2, 'c');
  binder(1, 2.0);
}

但是,请注意,虽然您可以将binder传递到任何需要可调用的上下文,但不能转换为指向函数的指针。