在通用lambda捕获中使用此指针 [英] Using a this pointer in a generic lambda capture

查看:118
本文介绍了在通用lambda捕获中使用此指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,其中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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆