为什么不允许使用=和this的lambda(但GCC接受)
Why is a lambda with = and this disallowed (yet GCC accepts)
我创建了一个简单的lambda,如下所示,它按预期工作(GCC 4.6.4和4.7.2-demo)。但后来我检查了标准,5.1.2-8明确禁止在lambda捕获中使用=
和this
。
如果lambda捕获包含捕获默认值,即=,则lambda捕获,并且它包含的每个标识符前面都应加上&。。
我是不是读错了什么,而这实际上是允许的(尽管这个例子明确显示这是禁止的)?如果没有,那么我很难理解为什么不允许这样做。同时,这是否意味着海湾合作委员会允许这样做是错误的?
#include <iostream>
#include <functional>
using namespace std;
struct sample {
int a;
std::function<int()> get_simple(int o) {
return [=,this]() {
return a + o;
};
}
};
int main() {
sample s;
auto f = s.get_simple(5);
s.a = 10;
cout << f() << endl; //prints 15 as expected
}
如果您已经通过设置[=]指定了默认捕获模式,则不需要捕获"this"字段。请参阅下面我明确传递"this"和o by值的地方。因此,警告告诉您,在这种情况下,您将多余地传递"this",因为当指定=或&作为默认捕获模式。因此,只有在未指定默认捕获模式时才指定"this"。请参见下文。
#include <iostream>
#include <functional>
using namespace std;
struct sample {
int a;
std::function<int()> get_simple(int o)
{
return [o,this]{ return a + o; };
}
};
int main() {
sample s;
auto f = s.get_simple(5);
s.a = 10;
cout << f() << endl; //prints 15 as expected
}
使用GCC 4.8.1中编译的代码和-Wall
标志,编译会发出以下警告:
main.cpp:在成员函数'std::function中sample::get_simple(int)':
main.cpp:10:19:警告:通过复制捕获"this"冗余显式通过复制捕获默认[默认启用]
return [=,this]() { ^
我认为这是GCC为了与C++11之前的标准lambdas兼容而制作的。
P0409R2现在允许这样做。
此外,P0806R2不赞成使用[=]
中的隐式this
。例如,尝试Clang++8 -std=c++2a
,您将收到[=]
的警告[-Wrecommended this capture]。
相关文章:
- 为什么lambda在clang上崩溃而不是在gcc上崩溃
- Visual C++: MSVC vs. GCC+CLANG: 处理 lambda 捕获类成员变量,正确的方法是什么?
- 为什么 GCC 和 clang 之间编译的 c++17 lambda 存在差异?
- 修改嵌套 lambda 中捕获的参数:gcc 与 clang?
- 在折叠表达式中扩展参数包作为 lambda 捕获的一部分 - gcc 与 clang
- GCC 6.x警告有关Lambda可见性
- 与lambda一起使用虚拟继承在初始化列表中捕获此问题的GCC错误
- 在折叠表达式中使用 lambda 时"Uninitialized captured reference"错误 - clang vs gcc
- 为什么将 lambda 用于非类型模板参数时 gcc 失败?
- 如何在 c++/gcc 中获取 lambda 函数名称?
- 如何获取在 gcc 5+ 上不起作用的 lambda 函数
- 使用 lambda 的错误 gcc 行为
- 将参数包扩展到具有折叠表达式的lambda -GCC与Clang
- 从值捕获的变量分配到lambda参数时,GCC编译器分割故障
- 如果对QObject::connect()使用lambda函数,则使用gcc编译失败
- 可能的 gcc 错误与 C++14 多态 lambda
- 在非“constexpr”上下文中的“constexpr”函数中使用lambda:clang vs gcc
- 与GCC/MSVC中的lambda转换构造函数不一致
- 为什么不允许使用=和this的lambda(但GCC接受)
- GCC在lambda函数中通过引用错误地捕获全局变量