Skip to content

Commit d470423

Browse files
authored
book: fix std::bind example with no return value (#309)
The `foo` in the std::bind example was declared to return `int` but had an empty body, which is undefined behavior (falling off the end of a non-void function). Make it return `a + b + c` and print the result so the example is correct and demonstrative. Fixes #305
1 parent f4edad7 commit d470423

2 files changed

Lines changed: 6 additions & 6 deletions

File tree

book/en-us/03-runtime.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,14 +214,14 @@ and then complete the call after the parameters are complete. e.g:
214214

215215
```cpp
216216
int foo(int a, int b, int c) {
217-
;
217+
return a + b + c;
218218
}
219219
int main() {
220220
// bind parameter 1, 2 on function foo,
221221
// and use std::placeholders::_1 as placeholder for the first parameter.
222-
auto bindFoo = std::bind(foo, std::placeholders::_1, 1,2);
222+
auto bindFoo = std::bind(foo, std::placeholders::_1, 1, 2);
223223
// when call bindFoo, we only need one param left
224-
bindFoo(1);
224+
std::cout << bindFoo(1) << std::endl; // outputs 4
225225
}
226226
```
227227

book/zh-cn/03-runtime.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,14 @@ int main() {
188188
189189
```cpp
190190
int foo(int a, int b, int c) {
191-
;
191+
return a + b + c;
192192
}
193193
int main() {
194194
// 将参数1,2绑定到函数 foo 上,
195195
// 但使用 std::placeholders::_1 来对第一个参数进行占位
196-
auto bindFoo = std::bind(foo, std::placeholders::_1, 1,2);
196+
auto bindFoo = std::bind(foo, std::placeholders::_1, 1, 2);
197197
// 这时调用 bindFoo 时,只需要提供第一个参数即可
198-
bindFoo(1);
198+
std::cout << bindFoo(1) << std::endl; // 输出 4
199199
}
200200
```
201201

0 commit comments

Comments
 (0)