This is the simplest example I could come up with that reproduces the problem.
template<class T>
struct X
{
static void foo()
{
static int z = 0;
[]{ z = 1; }();
}
};
int main()
{
X<int>::foo();
return 0;
}
I’ve tried it with MinGW 4.6 and 4.7, also g++ 4.6 in Ubuntu and all of them give me the link error “undefined reference to `z'”. So now that makes me wonder if this is even legal. VC10 has no problem with it.
It works if X is normal class instead of a template. Also, I don’t think it’s related to lambdas cause I get the error even if I replace the lambda with a local class.
g++ accepts the following, but VC++ does not:
Here
zis being captured so g++ does not complain about an undefined reference. However:zis not automatic storage.zcan therefore not be captured. g++ behavior is therefore incorrect, and VC++ is correct.In your code, that VC++ accepts and g++ does not:
zis accessed by VC++ as static storage, which is allowed in a lambda body. g++ apparently does not resolve the namezto the static variable declared above and therefore throws undefined reference, while it shouldn’t.tl;dr
It’s probably a bug in g++
Edit:
It is indeed a bug and is fixed in 4.7.