C++函数 - 闭包

void OnRecvSessionGracefullyShutdownNotify(WORD32 index)
{
    handleConnByIndex(index, [&](shared_ptr<http2_connection> conn)
    {
        conn->on_recv_session_gracefully_shutdown_notify();
    });
}

void handleConnByIndex(WORD32 index, std::function<void> vistor)
{
    if(cur_thread())
    {
        vistor(conn);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <functional>

using namespace std;
        
void handleIndex(int index, std::function<void()> vistor)
{
    if(index == 1)
    {
        vistor();
    }
}
void OnHandleIndex(int index)
{
    handleIndex(index, [&]()
    {
        cout<< "deal" <<endl;
        return;
    });
}

int main()
{
 
  OnHandleIndex(0);
  OnHandleIndex(1);
  return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28