Skip to content

Commit d472ff1

Browse files
committed
book: add text about auto as func args
Fixes #180
1 parent 7eeaa7a commit d472ff1

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

book/en-us/02-usability.md

+14-13
Original file line numberDiff line numberDiff line change
@@ -411,19 +411,20 @@ auto i = 5; // i as int
411411
auto arr = new auto(10); // arr as int *
412412
```
413413

414-
> **Note**: `auto` cannot be used for function arguments, so the following
415-
> is not possible to compile (considering overloading,
416-
> we should use templates):
417-
>
418-
> ```cpp
419-
> int add(auto x, auto y);
420-
>
421-
> 2.6.auto.cpp:16:9: error: 'auto' not allowed in function prototype
422-
> int add(auto x, auto y) {
423-
> ^~~~
424-
> ```
425-
>
426-
> In addition, `auto` cannot be used to derive array types:
414+
Since C++ 20, `auto` can even be used as function arguments. Consider
415+
the following example:
416+
417+
```cpp
418+
int add(auto x, auto y) {
419+
return x+y;
420+
}
421+
422+
auto i = 5; // type int
423+
auto j = 6; // type int
424+
std::cout << add(i, j) << std::endl;
425+
```
426+
427+
> **Note**: `auto` cannot be used to derive array types yet:
427428
>
428429
> ```cpp
429430
> auto auto_arr2[10] = {arr}; // illegal, can't infer array type

book/zh-cn/02-usability.md

+16-12
Original file line numberDiff line numberDiff line change
@@ -348,20 +348,24 @@ auto i = 5; // i 被推导为 int
348348
auto arr = new auto(10); // arr 被推导为 int *
349349
```
350350

351-
> **注意**:`auto` 不能用于函数传参,因此下面的做法是无法通过编译的(考虑重载的问题,我们应该使用模板):
352-
>
353-
> ```cpp
354-
> int add(auto x, auto y);
355-
>
356-
> 2.6.auto.cpp:16:9: error: 'auto' not allowed in function prototype
357-
> int add(auto x, auto y) {
358-
> ^~~~
359-
> ```
351+
从 C++ 20 起,`auto` 甚至能用于函数传参,考虑下面的例子:
352+
353+
354+
```cpp
355+
int add(auto x, auto y) {
356+
return x+y;
357+
}
358+
359+
auto i = 5; // 被推导为 int
360+
auto j = 6; // 被推导为 int
361+
std::cout << add(i, j) << std::endl;
362+
```
363+
360364
>
361-
> 此外,`auto` 还不能用于推导数组类型:
365+
> **注意**:`auto` 还不能用于推导数组类型:
362366
>
363367
> ```cpp
364-
> auto auto_arr2[10] = {arr}; // 错误, 无法推导数组元素类型
368+
> auto auto_arr2[10] = {arr}; // 错误, 无法推导数组元素类型
365369
>
366370
> 2.6.auto.cpp:30:19: error: 'auto_arr2' declared as array of 'auto'
367371
> auto auto_arr2[10] = {arr};
@@ -415,7 +419,7 @@ R add(T x, U y) {
415419
> 注意:typename 和 class 在模板参数列表中没有区别,在 typename 这个关键字出现之前,都是使用 class 来定义模板参数的。但在模板中定义有[嵌套依赖类型](http://en.cppreference.com/w/cpp/language/dependent_name#The_typename_disambiguator_for_dependent_names)的变量时,需要用 typename 消除歧义
416420
417421
418-
这样的代码其实变得很丑陋,因为程序员在使用这个模板函数的时候,必须明确指出返回类型。但事实上我们并不知道 `add()` 这个函数会做什么样的操作,获得一个什么样的返回类型
422+
这样的代码其实变得很丑陋,因为程序员在使用这个模板函数的时候,必须明确指出返回类型。但事实上我们并不知道 `add()` 这个函数会做什么样的操作,以及获得一个什么样的返回类型
419423
420424
在 C++11 中这个问题得到解决。虽然你可能马上会反应出来使用 `decltype` 推导 `x+y` 的类型,写出这样的代码:
421425

code/2/2.06.auto.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ class MagicFoo {
2121
}
2222
};
2323

24-
// wrong
25-
// int add(auto x, auto y) {
26-
// return x+y;
27-
// }
24+
int add(auto x, auto y) { // Supported in C++20
25+
return x+y;
26+
}
2827

2928
int main() {
3029
MagicFoo magicFoo = {1, 2, 3, 4, 5};
@@ -34,10 +33,11 @@ int main() {
3433
}
3534
std::cout << std::endl;
3635

37-
auto i = 5; // type int
38-
auto j = 6; // type int
39-
auto arr = new auto(10); // type int*
40-
// auto auto_arr2[10] = {arr};
41-
// std::cout << add(i, j) << std::endl;
36+
auto i = 5; // type int
37+
auto j = 6; // type int
38+
std::cout << add(i, j) << std::endl;
39+
40+
auto arr = new auto(10); // type int*
41+
// auto auto_arr2[10] = {arr}; // invalid
4242
return 0;
4343
}

0 commit comments

Comments
 (0)