The captured variable skins pass the variable to lambda. how to show?

Consider the following example:

int main() { int a = 100; std::cout<<[=,&a](int a,int b){return a+b;}(99,1); return 0; } 

Conclusion 101 instead of my expectation of 100 .

I can’t indicate like this [&a,=] , because it gives an error.

How to avoid hiding the name and link to the parameter. I know that changing the name is an option, but I'm curious. it will also be useful to refer to the standard

EDIT: I am using gcc 4.7.1

EDIT:

here is the ideone link showing the demo. i used c ++ 4.7.2 complier there
ideone

+7
c ++ c ++ 11
source share
3 answers

I could not find anything related to lambdas in the standard that would indicate that your results are the expected behavior. I agree with Andy in the comments that this is a bug in the GCC. GCC 4.7.2 on Linux, GCC 4.7.2 from MinGW and GCC 4.8.0 from MinGW give the same results as in the question, but VC ++ 10 and VC ++ 11 give the expected results.

You should consider the error message

+7
source share

Since the parameter does not have a scope that you can name, you cannot use the scope resolution operator to disambiguate, not this or any other similar means. Therefore, it is impossible to eliminate this scenario the way you want. You just don't have to give your variables horrible and conflicting names.

+3
source share

How to print the outer a here, but in the inner area?

 int a = 1; { int a = 2; cout << a << endl; } 

You either change the name of one of the variables or you do not execute.

The same goes for your lambda.

(I apologize that I cannot refer to such a standard as you requested - I do not have it.)

+1
source share

All Articles