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)
c ++ lambda c ++ 11 visual-c ++ visual-c ++ - 2010
Prasoon saurav
source share