This is a gcc bug. From [expr.prim.lambda]:
The lambda-expression’s compound-statement yields the function-body (8.4) of the function call operator,
but for purposes of name lookup (3.4), determining the type and value ofthis
(9.3.2) and transforming id-expressions
referring to non-static class members into class member access expressions using(*this)
(9.3.1), the compound-statement is considered in the context of the lambda-expression. [ Example:struct S1 { int x, y; int operator()(int); void f() { [=]()->int { return operator()(this->x + y); // equivalent to S1::operator()(this->x + (*this).y) // this has type S1* }; } };
—end example ]
Since in your example you capture this
, the name lookup should include class members of Example
, which should therefore find Example::foo
. The lookup performed is identical to what would happen if foo(x)
appeared in the context of the lambda-expression itself, that is if the code looked like:
void bar()
{
foo(x); // clearly Example::foo(x);
}
At least this bug has a very simple workaround as indicated in the question: just do this->foo(x);
.