Skip to content

Commit 78b1cdf

Browse files
book: correct description of function template deduction from auto (changkun#280)
1 parent 5c50dbc commit 78b1cdf

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

book/en-us/02-usability.md

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -418,17 +418,23 @@ auto i = 5; // i as int
418418
auto arr = new auto(10); // arr as int *
419419
```
420420

421-
Since C++ 20, `auto` can even be used as function arguments. Consider
422-
the following example:
421+
Since C++ 14, `auto` can even be used as function arguments in generic lambda expressions,
422+
and such functionality is generalized to normal functions in C++ 20.
423+
Consider the following example:
423424

424425
```cpp
425-
int add(auto x, auto y) {
426+
auto add14 = [](auto x, auto y) -> int {
427+
return x+y;
428+
}
429+
430+
int add20(auto x, auto y) {
426431
return x+y;
427432
}
428433

429434
auto i = 5; // type int
430435
auto j = 6; // type int
431-
std::cout << add(i, j) << std::endl;
436+
std::cout << add14(i, j) << std::endl;
437+
std::cout << add20(i, j) << std::endl;
432438
```
433439
434440
> **Note**: `auto` cannot be used to derive array types yet:
@@ -481,7 +487,7 @@ type z == type x
481487

482488
### tail type inference
483489

484-
You may think that when we introduce `auto`, we have already mentioned that `auto` cannot be used for function arguments for type derivation. Can `auto` be used to derive the return type of a function? Still consider an example of an add function, which we have to write in traditional C++:
490+
You may think that whether `auto` can be used to deduce the return type of a function. Still consider an example of an add function, which we have to write in traditional C++:
485491

486492
```cpp
487493
template<typename R, typename T, typename U>

book/zh-cn/02-usability.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,17 +354,22 @@ auto i = 5; // i 被推导为 int
354354
auto arr = new auto(10); // arr 被推导为 int *
355355
```
356356

357-
从 C++ 20 起,`auto` 甚至能用于函数传参,考虑下面的例子:
357+
从 C++ 14 起,`auto` 能用于 lambda 表达式中的函数传参,而 C++ 20 起该功能推广到了一般的函数。考虑下面的例子:
358358

359359

360360
```cpp
361-
int add(auto x, auto y) {
361+
auto add14 = [](auto x, auto y) -> int {
362362
return x+y;
363363
}
364364

365-
auto i = 5; // 被推导为 int
366-
auto j = 6; // 被推导为 int
367-
std::cout << add(i, j) << std::endl;
365+
int add20(auto x, auto y) {
366+
return x+y;
367+
}
368+
369+
auto i = 5; // type int
370+
auto j = 6; // type int
371+
std::cout << add14(i, j) << std::endl;
372+
std::cout << add20(i, j) << std::endl;
368373
```
369374
370375
>
@@ -413,7 +418,7 @@ type z == type x
413418

414419
### 尾返回类型推导
415420

416-
你可能会思考,在介绍 `auto` 时,我们已经提过 `auto` 不能用于函数形参进行类型推导,那么 `auto` 能不能用于推导函数的返回类型呢?还是考虑一个加法函数的例子,在传统 C++ 中我们必须这么写:
421+
你可能会思考, `auto` 能不能用于推导函数的返回类型呢?还是考虑一个加法函数的例子,在传统 C++ 中我们必须这么写:
417422

418423
```cpp
419424
template<typename R, typename T, typename U>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy