Reputation: 18411
Consider this code in VS2015:
int a,b;
[]
{
int a; // C4456: declaration of 'a' hides previous local declaration
};
Why a
in lambda giving such warning? It compiles fine in VS2013.
EDIT: Interestingly, (and incorrectly), following is not an error in VS2013:
[a]
{
int a; // No error, even if `a` is captured.
a++;
};
Upvotes: 2
Views: 362
Reputation: 87210
The first warning definitely looks like a compiler bug.
The second one isn't a bug since you're declaring it in a different scope. The variable is only captured, it is not declared by the capture.
Think about the function object this could generate
class foo {
foo(int a): a(a) {}
void operator()() {
int a;
}
int a;
};
There's no conflict between the two declarations of a
, and since the lambda compiles to something like this, that's why the capture doesn't care about the inner declaration.
Update: This is something completely different from
void foo(int a) {
int a;
}
because in the case of the lambda, it will get compiled to a class with an operator()
, and the captures will get passed in as constructor parameters, which is why they're in a different scope.
Upvotes: 3