Bug 67274

Summary: Inconsistent `this->` required when calling member function in a lambda capturing `this` through another function
Product: gcc Reporter: Vittorio Romeo <vittorio.romeo>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: daniel.kruegler, jaak, webrown.cpp
Priority: P3 Keywords: c++-lambda
Version: 6.0   
Target Milestone: 7.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Bug Depends on:    
Bug Blocks: 54367    

Description Vittorio Romeo 2015-08-19 14:12:37 UTC
Passing a `this`-capturing generic lambda (to a template function) that calls a member function of `this` without an explicit `this->` results in an error:

error: cannot call member function 'void Example::foo(int)' without object       call([this](auto x){ foo(x); });`

If the lambda is not generic, or if the lambda is not passed to any other function but called in place, it compiles without an explicit `this->`. 

For reference, clang 3.6+ is cool with the code in all situations.

---

Minimal example:
http://melpon.org/wandbox/permlink/M2eH3FUOentPfieM

template<typename TF>
void call(TF&& f)
{
    f(1);   
}

struct Example
{        
    void foo(int){ }

    void bar()
    {
        call([this](auto x){ foo(x); });
    }
};

int main()
{
    Example{}.bar();
    return 0;
}

---

StackOverflow question with more examples:
http://stackoverflow.com/questions/32097759
Comment 1 Vittorio Romeo 2015-08-19 14:13:34 UTC
More test cases, using minor variations to the code posted above:

With bar() = call([this](auto x){ foo(x); });
clang++ 3.6+ compiles.
g++ 5.2+ does not compile.

With bar() = call([this](auto x){ this->foo(x); });
clang++ 3.6+ compiles.
g++ 5.2+ compiles.

With bar() = call([this](int x){ foo(x); });
clang++ 3.6+ compiles.
g++ 5.2+ compiles.

With bar() = [this](auto x){ foo(x); }(1);
clang++ 3.6+ compiles.
g++ 5.2+ compiles.
Comment 2 Paolo Carlini 2017-10-02 09:23:28 UTC
Fixed in 7.1.0, we already have testcases.