MCPcopy Create free account
hub / github.com/changkun/modern-cpp-tutorial / main

Function main

code/3/3.2.cpp:25–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

23}
24
25int main() {
26
27 auto f = [](int value) {
28 std::cout << value << std::endl;
29 };
30 functional(f); // 函数指针调用
31 f(1); // lambda 表达式调用
32
33 // std::function 包装了一个返回值为 int, 参数为 int 的函数
34 std::function<int(int)> func = foo2;
35
36 int important = 10;
37 std::function<int(int)> func2 = [&](int value) -> int {
38 return 1+value+important;
39 };
40 std::cout << func(10) << std::endl;
41 std::cout << func2(10) << std::endl;
42
43
44 // 将参数1,2绑定到函数 foo 上,但是使用 std::placeholders::_1 来对第一个参数进行占位
45 auto bindFoo = std::bind(foo3, std::placeholders::_1, 1,2);
46 // 这时调用 bindFoo 时,只需要提供第一个参数即可
47 bindFoo(1);
48
49
50 return 0;
51}

Callers

nothing calls this directly

Calls 3

functionalFunction · 0.85
fFunction · 0.85
funcFunction · 0.85

Tested by

no test coverage detected