Why Can't Member Variables Be Used Unqualified In A Lambda If Non-Captured Local Variables Exist?

Viewed 219

Simple example:

class Foo {
    int x;

    void bar(int x) {
        [this]() -> void {
            x = 6;
        }();
    }
};

This doesn't compile on GCC, Clang, MVC, or ICC (see it live). If I change void bar(int x) to void bar(int y), or if I change x = 6; to this->x = 6; then it works fine.

This doesn't make sense to me. The local variable x from the call to bar is deliberately not being captured in the lambda. The only x that makes sense is Foo's member variable.

Question: Is this the expected behavior, and iff so, please explain why?

2 Answers
Related