mirror of
https://github.com/changkun/modern-cpp-tutorial.git
synced 2025-12-17 12:44:40 +03:00
@@ -126,13 +126,9 @@ initialize it in the expression.
|
|||||||
|
|
||||||
In the previous section, we mentioned that the `auto` keyword cannot be used
|
In the previous section, we mentioned that the `auto` keyword cannot be used
|
||||||
in the parameter list because it would conflict with the functionality of the template.
|
in the parameter list because it would conflict with the functionality of the template.
|
||||||
But Lambda expressions are not ordinary functions, so Lambda expressions are not templated.
|
But lambda expressions are not regular functions, without further specification on the typed parameter list, lambda expressions cannot utilize templates. Fortunately, this trouble
|
||||||
This has caused us some trouble: the parameter table cannot be generalized,
|
only exists in C++11, starting with C++14. The formal parameters of the lambda function
|
||||||
and the parameter table type must be clarified.
|
can use the `auto` keyword to utilize template generics:
|
||||||
|
|
||||||
Fortunately, this trouble only exists in C++11, starting with C++14.
|
|
||||||
The formal parameters of the Lambda function can use the `auto` keyword
|
|
||||||
to generate generic meanings:
|
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
void lambda_generic() {
|
void lambda_generic() {
|
||||||
|
|||||||
@@ -107,11 +107,9 @@ void lambda_expression_capture() {
|
|||||||
### 泛型 Lambda
|
### 泛型 Lambda
|
||||||
|
|
||||||
上一节中我们提到了 `auto` 关键字不能够用在参数表里,这是因为这样的写法会与模板的功能产生冲突。
|
上一节中我们提到了 `auto` 关键字不能够用在参数表里,这是因为这样的写法会与模板的功能产生冲突。
|
||||||
但是 Lambda 表达式并不是普通函数,所以 Lambda 表达式并不能够模板化。
|
但是 Lambda 表达式并不是普通函数,所以在没有明确指明参数表类型的情况下,Lambda 表达式并不能够模板化。
|
||||||
这就为我们造成了一定程度上的麻烦:参数表不能够泛化,必须明确参数表类型。
|
幸运的是,这种麻烦只存在于 C++11 中,从 C++14 开始,Lambda 函数的形式参数可以使用 `auto`
|
||||||
|
关键字来产生意义上的泛型:
|
||||||
幸运的是,这种麻烦只存在于 C++11 中,从 C++14 开始,
|
|
||||||
Lambda 函数的形式参数可以使用 `auto` 关键字来产生意义上的泛型:
|
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
auto add = [](auto x, auto y) {
|
auto add = [](auto x, auto y) {
|
||||||
|
|||||||
Reference in New Issue
Block a user