mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-16 20:27:08 +03:00
@@ -411,19 +411,20 @@ auto i = 5; // i as int
|
||||
auto arr = new auto(10); // arr as int *
|
||||
```
|
||||
|
||||
> **Note**: `auto` cannot be used for function arguments, so the following
|
||||
> is not possible to compile (considering overloading,
|
||||
> we should use templates):
|
||||
>
|
||||
> ```cpp
|
||||
> int add(auto x, auto y);
|
||||
>
|
||||
> 2.6.auto.cpp:16:9: error: 'auto' not allowed in function prototype
|
||||
> int add(auto x, auto y) {
|
||||
> ^~~~
|
||||
> ```
|
||||
>
|
||||
> In addition, `auto` cannot be used to derive array types:
|
||||
Since C++ 20, `auto` can even be used as function arguments. Consider
|
||||
the following example:
|
||||
|
||||
```cpp
|
||||
int add(auto x, auto y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
auto i = 5; // type int
|
||||
auto j = 6; // type int
|
||||
std::cout << add(i, j) << std::endl;
|
||||
```
|
||||
|
||||
> **Note**: `auto` cannot be used to derive array types yet:
|
||||
>
|
||||
> ```cpp
|
||||
> auto auto_arr2[10] = {arr}; // illegal, can't infer array type
|
||||
|
||||
@@ -348,20 +348,24 @@ auto i = 5; // i 被推导为 int
|
||||
auto arr = new auto(10); // arr 被推导为 int *
|
||||
```
|
||||
|
||||
> **注意**:`auto` 不能用于函数传参,因此下面的做法是无法通过编译的(考虑重载的问题,我们应该使用模板):
|
||||
从 C++ 20 起,`auto` 甚至能用于函数传参,考虑下面的例子:
|
||||
|
||||
|
||||
```cpp
|
||||
int add(auto x, auto y) {
|
||||
return x+y;
|
||||
}
|
||||
|
||||
auto i = 5; // 被推导为 int
|
||||
auto j = 6; // 被推导为 int
|
||||
std::cout << add(i, j) << std::endl;
|
||||
```
|
||||
|
||||
>
|
||||
> **注意**:`auto` 还不能用于推导数组类型:
|
||||
>
|
||||
> ```cpp
|
||||
> int add(auto x, auto y);
|
||||
>
|
||||
> 2.6.auto.cpp:16:9: error: 'auto' not allowed in function prototype
|
||||
> int add(auto x, auto y) {
|
||||
> ^~~~
|
||||
> ```
|
||||
>
|
||||
> 此外,`auto` 还不能用于推导数组类型:
|
||||
>
|
||||
> ```cpp
|
||||
> auto auto_arr2[10] = {arr}; // 错误, 无法推导数组元素类型
|
||||
> auto auto_arr2[10] = {arr}; // 错误, 无法推导数组元素类型
|
||||
>
|
||||
> 2.6.auto.cpp:30:19: error: 'auto_arr2' declared as array of 'auto'
|
||||
> auto auto_arr2[10] = {arr};
|
||||
@@ -415,7 +419,7 @@ R add(T x, U y) {
|
||||
> 注意:typename 和 class 在模板参数列表中没有区别,在 typename 这个关键字出现之前,都是使用 class 来定义模板参数的。但在模板中定义有[嵌套依赖类型](http://en.cppreference.com/w/cpp/language/dependent_name#The_typename_disambiguator_for_dependent_names)的变量时,需要用 typename 消除歧义
|
||||
|
||||
|
||||
这样的代码其实变得很丑陋,因为程序员在使用这个模板函数的时候,必须明确指出返回类型。但事实上我们并不知道 `add()` 这个函数会做什么样的操作,获得一个什么样的返回类型。
|
||||
这样的代码其实变得很丑陋,因为程序员在使用这个模板函数的时候,必须明确指出返回类型。但事实上我们并不知道 `add()` 这个函数会做什么样的操作,以及获得一个什么样的返回类型。
|
||||
|
||||
在 C++11 中这个问题得到解决。虽然你可能马上会反应出来使用 `decltype` 推导 `x+y` 的类型,写出这样的代码:
|
||||
|
||||
|
||||
@@ -21,10 +21,9 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
// wrong
|
||||
// int add(auto x, auto y) {
|
||||
// return x+y;
|
||||
// }
|
||||
int add(auto x, auto y) { // Supported in C++20
|
||||
return x+y;
|
||||
}
|
||||
|
||||
int main() {
|
||||
MagicFoo magicFoo = {1, 2, 3, 4, 5};
|
||||
@@ -34,10 +33,11 @@ int main() {
|
||||
}
|
||||
std::cout << std::endl;
|
||||
|
||||
auto i = 5; // type int
|
||||
auto j = 6; // type int
|
||||
auto arr = new auto(10); // type int*
|
||||
// auto auto_arr2[10] = {arr};
|
||||
// std::cout << add(i, j) << std::endl;
|
||||
auto i = 5; // type int
|
||||
auto j = 6; // type int
|
||||
std::cout << add(i, j) << std::endl;
|
||||
|
||||
auto arr = new auto(10); // type int*
|
||||
// auto auto_arr2[10] = {arr}; // invalid
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user