【发布时间】:2019-03-17 07:13:40
【问题描述】:
鉴于 boost::bind 或 std:: 等价物,我可以做到这一点:
int f(int a, int b)
{
return a + b;
}
auto f_two = boost::bind(f, 1, 1);
所以f_two() 将通过有效调用一个中间函数来返回 2,该函数通过任何实现机制调用f(1, 1),可能类似于:
double f_two_caller()
{
return f(stored_arg_1, stored_arg_2);
}
但是,我的用例是我想绑定一个前缀函数,所以我可以说:
auto f_print = boost::bind(printf, "Hello, world!\n");
auto f_print_and_two = boost::bind_with_prefix(f, f_print, 1, 1);
所以f_print_and_two() 有效执行:
double f_print_and_two_caller()
{
f_print(f_print.stored_arg_1);
return f(stored_arg_1, stored_arg_2);
}
我确信这种技术有一个合适的名称可以用来查找解决方案,但我现在想不出来......
【问题讨论】:
-
lambdas 会为你工作吗?
-
@HolyBlackCat 我将用c++11 重新标记,因为我可以达到那么高但不能更高,所以 lambdas 可能会起作用。注意
f()不能修改,我想把最终的解决方案包装成一个mixin类之类的。 -
最近我偶然发现了
boost::hof::decorate,它看起来也可以解决问题。
标签: c++ c++11 boost function-binding