Using a this pointer in a generic lambda capture

左心房为你撑大大i 提交于 2019-12-23 16:07:42

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/31673204/using-a-this-pointer-in-a-generic-lambda-capture

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!