11//
2- // 3.2.cpp
2+ // 3.2.function.wrap.cpp
3+ // chapter 03 runtime enhancement
34// modern c++ tutorial
45//
56// created by changkun at changkun.de
67// https://github.com/changkun/modern-cpp-tutorial
78//
8- // std::function std::bind
99
1010#include < functional>
1111#include < iostream>
1212
13- using foo = void (int ); // 定义函数指针
13+ using foo = void (int ); // function pointer
1414void functional (foo f) {
1515 f (1 );
1616}
@@ -24,29 +24,28 @@ int foo3(int a, int b, int c) {
2424}
2525
2626int main () {
27-
27+
2828 auto f = [](int value) {
2929 std::cout << value << std::endl;
3030 };
31- functional (f); // 函数指针调用
32- f (1 ); // lambda 表达式调用
33-
34- // std::function 包装了一个返回值为 int, 参数为 int 的函数
31+ functional (f); // call by function pointer
32+ f (1 ); // call by lambda expression
33+
34+ // std::function wraps a function that take int paremeter and returns int value
3535 std::function<int (int )> func = foo2;
36-
36+
3737 int important = 10 ;
3838 std::function<int (int )> func2 = [&](int value) -> int {
3939 return 1 +value+important;
4040 };
4141 std::cout << func (10 ) << std::endl;
4242 std::cout << func2 (10 ) << std::endl;
43-
44-
45- // 将参数1,2绑定到函数 foo 上,但是使用 std::placeholders::_1 来对第一个参数进行占位
43+
44+ // bind parameter 1, 2 on function foo, and use std::placeholders::_1 as placeholder
45+ // for the first parameter.
4646 auto bindFoo = std::bind (foo3, std::placeholders::_1, 1 ,2 );
47- // 这时调用 bindFoo 时,只需要提供第一个参数即可
47+ // when call bindFoo, we only need one param left
4848 bindFoo (1 );
49-
50-
49+
5150 return 0 ;
5251}
0 commit comments