C2326 function cannot access bar when defining lambda inside local class

I came across some weird compiler error related to local classes and lambdas. I narrowed it down to the following example:

int main() { class test { void foo(int bar) { auto lambda = [=] (int) { return bar; }; } }; return 0; } 

And VS10 says:

error C2326: 'void main :: test :: foo (int)': the function cannot access the bar

Are there any restrictions on using lambdas in local classes or is this a bug in the compiler?

Thank you four for your help.

+4
source share
2 answers

I can confirm that this happens in VS10 only when the class is defined in the function and the lambda takes a parameter. Such problems do not appear in g ++.

I believe that this is really a mistake, if you open a defect, please refer to it here so that we can follow it (otherwise let me know and I will open the defect).

+2
source

And you tried

 int main() { class test { void foo(int bar) { auto lambda = [bar] (int) { return bar; }; } }; return 0; } 

Perhaps the implementation of lambdas in VS10 is not complete, because your example works fine on g ++ 4.5 - http://www.ideone.com/5xQpz

0
source

Source: https://habr.com/ru/post/1339538/


All Articles