在通用lambda捕获中使用此指针 [英] Using a this pointer in a generic lambda capture
问题描述
我有一个问题,其中Clang(3.6)和G ++(5.1)有不同的意见:
#include< ;功能>
struct X
{
X()
{
std :: function< void(int) f = [this](auto x){foo(x);};
}
void foo(int x){}
};
int main(){}
Clang接受这一点, :
错误:无法调用成员函数'void X :: foo(int)'没有对象
如果我调用 this-> foo(x)
注意:lambda签名中的auto和转换为std :: function<>的都是需要触发此情况。
如果我调用this-> foo
考虑到它在gcc 5.2中编译,clang是一个正确的您的具体情况。它看起来像只是一个bug在gcc 5.1。
此外,它也有直观的意义,这个
I have an issue in which Clang (3.6) and G++ (5.1) have a differing opinion:
#include <functional>
struct X
{
X()
{
std::function<void (int)> f = [this](auto x){foo(x);};
}
void foo(int x){}
};
int main(){}
Clang accepts this, whereas G++ states:
error: cannot call member function ‘void X::foo(int)’ without object
Both compilers accept it if I call this->foo(x)
directly instead, but I'd rather know who's right.
Note: both the "auto" in the lambda signature and the conversion to a std::function<> are required to trigger this case.
Both compilers accept it if I call this->foo(x) directly instead, but I'd rather know who's right.
Considering it compiles in gcc 5.2, clang is the one correct in your specific case. It looks like it was just a bug in gcc 5.1. gcc 6.0 also compiles this fine.
Plus it makes intuitive sense, this
should be implied.
这篇关于在通用lambda捕获中使用此指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!