Why can't a data member be in a lambda capture list

c++, c++11, lambda

Solution

You capture class members by saying `this` in the capture list. This has nothing to do with the fact that the member is `const`.

Example:

#include <iostream>

struct Foo
{
    const int a = 0;
    int b;
    Foo() : b{42} {
        auto f = [this]() { std::cout << a << " " << b << std::endl; };
//                ^^^^ 
        f();
    }
};

int main() {
    Foo x;
}

Problem

I have a class `foo` that has `bar` as a member variable. In another member function of the class, I'm writing a lambda function: ``` [bar](void){} ``` But I can't include `bar` in the capture list. Why is that?

Original source