为什么 lambda 参数中不允许使用 auto?
Why is auto not allowed in lambda parameter?
当我使用以下签名编写 lambda 定义时:
auto lambda = [&] (auto i){
};
我收到以下编译器错误:
error: 'auto' not allowed in lambda parameter
当我将类型从 auto
更改为 int
时,错误消失了。
我不确定为什么编译器可以推断出 lambda 的类型,而不是它的参数类型,它应该在调用 lambda 时知道?
我正在尝试了解此限制背后的原因。
I am not sure why the compiler can deduce the type of a lambda, but
not its parameter type, which should be known to it at the time of the
invocation of the lambda?
它可以,但仅限于 C++14.
auto lambda = [&] (auto i) { };
此代码完全合法,因为 C++14 并调用了 generic lambda。
不幸的是,通用 lambdas 在 C++14 之前不可用,因此,如果您需要使用它们,C++14 需要支持。
当我使用以下签名编写 lambda 定义时:
auto lambda = [&] (auto i){
};
我收到以下编译器错误:
error: 'auto' not allowed in lambda parameter
当我将类型从 auto
更改为 int
时,错误消失了。
我不确定为什么编译器可以推断出 lambda 的类型,而不是它的参数类型,它应该在调用 lambda 时知道?
我正在尝试了解此限制背后的原因。
I am not sure why the compiler can deduce the type of a lambda, but not its parameter type, which should be known to it at the time of the invocation of the lambda?
它可以,但仅限于 C++14.
auto lambda = [&] (auto i) { };
此代码完全合法,因为 C++14 并调用了 generic lambda。
不幸的是,通用 lambdas 在 C++14 之前不可用,因此,如果您需要使用它们,C++14 需要支持。