I encountered a compilation error in VS2010, and I don’t know if it should be considered a compiler bug or not:
I simplified the scenario as much as possible: a template class is declared a friend of some class, and tries to access the private members of the friend inside a lambda function. Here’s the code:
class Foo {
template<typename T> friend class Bar;
int priv;
};
template<typename T>
class Bar {
public:
void func() {
Foo foo;
foo.priv = 17; // compiles
auto lambda_func = [](Foo& _foo) { _foo.priv = 17; }; // doesn't compile
}
};
void test() {
Bar<int> bar;
bar.func();
}
Note that this only happens if Bar is a template class.
This compiles fine in g++ 4.6 and 4.7. I think it’s legal too — the lambda should have as much access as the function it is defined in.
The C++11 standard, 5.1.2p7 says (emphasis added):
I read that as meaning that anything valid within the enclosing block is valid in the lambda body.
MSVC2010 has other bugs with lambdas, so it doesn’t surprise me that it fails to compile this case.