material/program/c_cpp/STL/function.md

25 lines
450 B
Markdown
Raw Normal View History

2025-01-10 22:29:51 +08:00
# function 包装器
提供更加一致的接口,避免多次实例化
对于调用特征标(call signature)一样的函数使用function进行包装
```cpp
std::function<double(int)> fun1;
```
+ 可接受任何函数指针函数对象lambda表达式
```cpp
template <typename T>
void print(T f, int a) {
std::cout << f(a) << std::endl;
}
double f1(int a) {
return a/1.0;
}
std::function<double(int)> fun1=f1;
print(fun1, 10);
```