Consider the following code:
class A
{
public:
void foo()
{
auto functor = [this]()
{
A * a = this;
auto functor = [a]() // The compiler won't accept "this" instead of "a"
{
a->bar();
};
};
}
void bar() {}
};
In VC2010, using this
instead of a
lead to compilation errors. Among others:
1>main.cpp(20): error C3480: '`anonymous-namespace'::<lambda0>::__this': a lambda capture variable must be from an enclosing function scope
1>main.cpp(22): error C3493: 'this' cannot be implicitly captured because no default capture mode has been specified
Which I don't understand. Does it mean it doesn't know if it should use a reference or copy it? When trying to use &this
to force referencing, it also says:
1>main.cpp(20): error C3496: 'this' is always captured by value: '&' ignored
The temporary is not that annoying, but for the sake of curiosity, is there a way to get rid of it? What goes on when this
is given to a lambda?