函数对象包装器function和bind机制

简介: 函数对象包装器function和bind机制

1 解决什么问题

主要是为函数提供了一种容器,将函数当做对象来使用。

2 基本用法

采用function需要包含头文件 #include <functional.h>

int test(int n){
  return n;
}
test(1);
等价于
std::function<int(int)> f = test;  
f(123);

3 C++支持四种函数的封装

1 普通函数 2 匿名函数 3 类的成员函数 4仿函数

对于普通函数上面的用法就是普通函数。

<font size="3" color="blue">匿名函数</font>
std::functional<int(int)> f2 = [](int n) -> int {
  //函数体
};
调用  f2(123);

类的成员函数

class Ctest{
public:
  int test(int){
    //业务处理
  };
};
std::functional<int(CTest*, int)> f3 = &CTest::test;
CTest t;
f3(&t, 123);

经常容易出错的是不要CTest。

仿函数

重载了()运算符的函数叫仿函数

CTest {
 public:
 int operator()(int n){
 //业务处理
 }
 };
CTest t;
 t(1234);
 等价于
 std::functional<int(CTest*, int) f4= &CTest::operator();
 CTest t;
 f4(&t, 1234);

4 bind机制

对于function来说是将函数当成对象来使用,而bind机制是将函数参数和函数绑定在一起当成对象来使用。

基本用法:

int test(int a, int b, int c){
  //业务处理
};

实现一个函数参数可能比较多。

auto a = std::bind(test, 1,2,3);

test(函数)+参数绑定在一起称为一个对象来使用

调用:  a()  当成给一个对象使用

当然对于后面的参数1,2,3,也可以采用站位符号来使用。例如

auto f2 = std::bind(test, 1, std::placeholders::_1, 3);
f2(2);
或者
auto f2 = std::bind(test, std::placeholders_2, std::placeholders::_1, 3);

f2(1,2); //1对于站位符std::placeholders::_1

目录
相关文章
|
4天前
|
数据可视化 开发者 索引
详解Wireshark LUA插件函数:function p_myproto.dissector(buffer, pinfo, tree)
在 Wireshark 中,LUA 插件通过 `function p_myproto.dissector(buffer, pinfo, tree)` 扩展协议解析能力,解析自定义应用层协议。参数 `buffer` 是 `PacketBuffer` 类型,表示原始数据包内容;`pinfo` 是 `ProtoInfo` 类型,包含数据包元信息(如 IP 地址、协议类型等);`tree` 是
8 1
|
2月前
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
【Azure Durable Function】PowerShell Activity 函数遇见 Newtonsoft.Json.JsonReaderException: The reader's MaxDepth of 64 has been exceeded.
|
2月前
[Azure Developer]把Azure Function中ILogger对象静态化为静态方法提供日志记录
[Azure Developer]把Azure Function中ILogger对象静态化为静态方法提供日志记录
|
2月前
|
安全 JavaScript 应用服务中间件
【Azure Function App】如何修改Azure函数应用的默认页面呢?
【Azure Function App】如何修改Azure函数应用的默认页面呢?
|
2月前
|
SQL JavaScript 前端开发
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
【Azure 应用服务】Azure JS Function 异步方法中执行SQL查询后,Callback函数中日志无法输出问题
|
2月前
|
JSON 数据格式 Python
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
【Azure 应用服务】Azure Function Python函数中,如何获取Event Hub Trigger的消息Event所属于的PartitionID呢?
|
2月前
|
C++ Python
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
【Azure 应用服务】Azure Function Python函数部署到Azure后遇见 Value cannot be null. (Parameter 'receiverConnectionString') 错误
|
2月前
|
C# C++ Python
【Azure 应用服务】Azure Durable Function(持久函数)在执行Activity Function时候,因为调用函数名称错误而导致长时间无响应问题
【Azure 应用服务】Azure Durable Function(持久函数)在执行Activity Function时候,因为调用函数名称错误而导致长时间无响应问题
|
2月前
【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
【Azure 应用服务】Azure Function Timer触发函数加上Singleton后的问题
|
2月前
|
消息中间件 Kafka 网络安全
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验
【Azure 应用服务】本地创建Azure Function Kafka Trigger 函数和Kafka output的HTTP Trigger函数实验

热门文章

最新文章