Kaz Dragon
Kaz Dragon

Reputation: 6809

Using a this pointer in a generic lambda capture

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.

Upvotes: 3

Views: 360

Answers (1)

Blindy
Blindy

Reputation: 67564

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.

Upvotes: 1

Related Questions