std::function 可包装任意可调用对象,实现统一接口;std::bind 能绑定部分参数并重排顺序,二者结合常用于回调、事件系统等场景,提升灵活性。

在C++函数式编程中,std::function 和 std::bind 是两个非常实用的工具,它们让函数对象、回调机制和延迟调用变得更灵活。下面通过具体例子说明它们的基本用法和常见技巧。
std::function 是一个通用的函数包装器,可以保存、复制和调用任何可调用的目标,比如普通函数、lambda表达式、函数对象或成员函数指针。
基本语法:
std::function
立即学习“C++免费学习笔记(深入)”;
示例:
#include <functional><br>
#include <iostream><br><br>
double add(double a, double b) {<br>
return a + b;<br>
}<br><br>
std::function<double(double, double)> func = add;<br>
std::cout << func(2.5, 3.0) << std::endl; // 输出 5.5
std::function<int(int, int)> max_func = [](int a, int b) {<br>
return a > b ? a : b;<br>
};<br>
std::cout << max_func(4, 7) << std::endl; // 输出 7
将 std::function 作为参数传递,实现灵活的回调机制:
void process_data(std::function<void(int)> callback) {<br>
for (int i = 0; i < 5; ++i) {<br>
callback(i);<br>
}<br>
}<br><br>
process_data([](int x) { std::cout << "处理: " << x << std::endl; });
std::bind 可以把函数的部分参数预先绑定,生成一个新的可调用对象,常用于适配函数签名或固定某些参数。
基本语法:
std::bind(函数名, 参数1, 参数2...)
占位符 std::placeholders::_1, _2 等表示运行时传入的参数位置。
例如,有一个三参数函数,想固定前两个参数:
#include <functional><br><br>
int multiply(int a, int b, int c) {<br>
return a * b * c;<br>
}<br><br>
auto bind_first_two = std::bind(multiply, 2, 3, std::placeholders::_1);<br>
std::cout << bind_first_two(4) << std::endl; // 相当于 multiply(2, 3, 4) → 24
使用占位符重新排列参数顺序:
auto reorder = std::bind(multiply, std::placeholders::_2, std::placeholders::_1, 5);<br> std::cout << reorder(2, 3) << std::endl; // 相当于 multiply(3, 2, 5) → 30
必须传入对象实例(或指针)作为第一个参数:
struct Calculator {<br>
int add(int a, int b) { return a + b; }<br>
};<br><br>
Calculator calc;<br>
auto add_bind = std::bind(&Calculator::add, &calc, std::placeholders::_1, std::placeholders::_2);<br>
std::cout << add_bind(3, 4) << std::endl; // 输出 7
通常会将 std::bind 的结果赋给 std::function,以便统一管理类型或作为回调传参:
std::function<int(int)> func = std::bind(multiply, 2, std::placeholders::_1, 10);<br> std::cout << func(3) << std::endl; // 相当于 multiply(2, 3, 10) → 60
这种组合特别适合事件系统、线程任务或需要注册不同调用形式的场景。
基本上就这些。std::function 提供了类型擦除的调用接口,std::bind 则增强了函数的复用性和灵活性。虽然 C++11 之后 lambda 更常用,但在需要参数重排或长期保存绑定逻辑时,std::bind 仍有其价值。不复杂但容易忽略的是占位符的作用域和绑定对象的生命周期管理。
以上就是C++ std::function和std::bind用法_C++函数式编程技巧的详细内容,更多请关注php中文网其它相关文章!
编程怎么学习?编程怎么入门?编程在哪学?编程怎么学才快?不用担心,这里为大家提供了编程速学教程(入门课程),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号