Bug 67274 - Inconsistent `this->` required when calling member function in a lambda capturing `this` through another function
Summary: Inconsistent `this->` required when calling member function in a lambda captu...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: 7.0
Assignee: Not yet assigned to anyone
URL:
Keywords: c++-lambda
Depends on:
Blocks: lambdas
  Show dependency treegraph
 
Reported: 2015-08-19 14:12 UTC by Vittorio Romeo
Modified: 2022-03-11 00:32 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.