I could not get the following test code work in XCode 4.5.2:
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<int> v = {1, 2, 3};
for_each(v.begin(), v.end(), [](int e){cout<<e;});
return 0;
}
The syntax-checker says before the [] sign, that “Expected expression”.
Does that mean that XCode 4.5.2 does not support lambdas?
Is there any project setup, compilation flag or something else which can enable lambdas?
Is there any chance that my compiler is not the correct version? (Apple LLVM compiler 4.1)
I have read a lot on SO and on other forums, and they say the version numbers are confusing but everybody seemed to agree that XCode 4.4+ supports lambdas. What is the truth here?
Thanks for any help!
Xcode 4.5.2 does support lambdas. Are you sure you’re in C++11 mode? Go to your build settings. There’s a C++ Language Dialect option. It should be set to either
C++11orGNU++11. Anything else will mean you’re not using C++11 and, therefore, don’t have lambdas.