Lambda expression (MSVC ++ vs g ++)

I have the following code

#include <algorithm> #include <iostream> #include <vector> #include <functional> int main() { typedef std::vector<int> Vector; int sum=0; Vector v; for(int i=1;i<=10;++i) v.push_back(i); std::tr1::function<double()> l=[&]()->double{ std::for_each(v.begin(),v.end(),[&](int n){sum += n; //Error Here in MSVC++}); return sum; }; std::cout<<l(); std::cin.get(); } 

The above code causes an error with MSVC++ 10 , while it compiles with g++ 4.5 . An error occurred: 1 IntelliSense: invalid reference to an outer-scope local variable in a lambda body c:\users\super user\documents\visual studio 2010\projects\lambda\lambda.cpp 19 46 lambda

So, is there another way to access the appearance variable sum without explicitly creating a new variable inside the local lambda expression (inside std::for_each )?

In g++ 4.5 code compiles fine. Does the standard (project n3000) mean this? (I don't have a copy of the C ++ standard - 0x (1x?) At present)

+6
c ++ lambda c ++ 11 visual-c ++ visual-c ++ - 2010
source share
4 answers

Did you really try to compile the code in the question? Visual C ++ 2010 accepts the code as it is (with the comment removed, obviously), and successfully compiles the code without errors.

The โ€œerrorโ€ you see is not a compilation error, but an IntelliSense error. IntelliSense error checking leads to many false positives (over the past few months I have reported several errors in Microsoft Connect); in this case, IntelliSense incorrectly says that it is a mistake if it is not.

You have two options: you can ignore IntelliSense false positives or you can disable IntelliSense error checking (right-click the Error List window and clear the Show IntelliSense Errors check box).

In any case, these IntelliSense errors in no way hinder compilation.

+15
source share

Regardless of whether the VC is incorrect or correct, this is a bad style that you have specified outside of your (external) lambda. Since you are returning the value of the sum, there is no need to change the value of the external variable inside the loop. Instead, you should have:

 int sum = 0; std::for_each(v.begin(),v.end(),[&](int n){sum += n;}); return sum; 

It is possible that nested lambdas also confuse VC. I would say that it is too much to have nested lambdas and makes less clear code.

+1
source share

I think you might have to explicitly declare a closure over sum , for example:

 std::for_each(v.begin(),v.end(),[&sum](int n){sum += n;}); 

In general, you should be able to implicitly capture variables in the local area, but only until the lambda works in the same area. Perhaps because you assign your lambda to the var function and execute it later (instead of just running it directly), MSVC isnโ€™t smart enough to realize that this condition is true - in the end, you can pass l and execute it in some other area - so this requires an explicit capture declaration.

0
source share

I think the only problem you are facing is the red wave of size ant ... SINCE Microsoft released the compiler earlier, and soon the standards body changed the rule to search for the name ... so intellisense does not match the date ..... ...

WITH TRYING WITH THIS IDEA ..... CHILD ...

 #include <algorithm> #include <iostream> #include <vector> #include <functional> int main() { typedef std::vector<int> Vector; int sum=0; Vector v; for(int i=1;i<=10;++i) v.push_back(i); std::tr1::function<double()> l=[&]()->double{ int *y; y=&sum; std::for_each(v.begin(),v.end(),[&](int n){*y += n; }); return sum; }; std::cout<<l(); std::cin.get(); } 
0
source share

All Articles