I am trying to find if all the elements of a vector y is equal to 1. The following code works fine in Visual Studio but with g++ in linux (g++ -std=c++0x) it gives me this error: expected primary-expression before ‘[’ token
bool x = all_of(y.begin(), y.end(), [](unsigned char j) {return j == 1;});
Any help would be appreciated.
My gcc version is: g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)
3 Answers 3
Lambdas are not supported in GCC 4.4.
Upgrade your compiler. You need GCC 4.5 or higher, but get to modern times if you can.
1 Comment
Your compiler not support lambda expression. Compilers do not support all features that come with C++11 or new incoming standards. Therefore you need to check which standards the compiler supports. Screenshot of compilation on GCC 4.4.7
You can see if I select gcc-4.4.7 same error( lambda expression error) but if I select gcc-4.5.3
Screenshot of compilation on GCC 4.5.2
No error. In summary, You have to change your compiler(>= gcc-4.5 ) to use lamda expression.
C++ Standards Support in GCC
Comments
Lambdas are not supported in GCC 4.4. You can upgrade your compiler to version 4.5 or above, or use a function:
bool compFun(int i) {
return i == 1;
}
...
bool res = all_of(a.begin(), a.end(), compFun);
-std=c++0xThis suggests an old compiler. What version of GCC is that?