1

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)

asked Jan 6, 2017 at 17:00
9
  • 1
    What version of g++ are you using? Commented Jan 6, 2017 at 17:00
  • 1
    Are you sure you enabled c++11? Or perhaps you have an ancient version of g++. Commented Jan 6, 2017 at 17:01
  • -std=c++0x This suggests an old compiler. What version of GCC is that? Commented Jan 6, 2017 at 17:05
  • 2
    @Ali That's almost five years old. Time to upgrade. Commented Jan 6, 2017 at 17:06
  • 2
    Use an actual function/function pointer Commented Jan 6, 2017 at 17:15

3 Answers 3

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.

answered Jan 6, 2017 at 17:06
Sign up to request clarification or add additional context in comments.

1 Comment

@GillBates you mock, but Turbo C++ is coming back, man. Just like Disco.
2

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

answered Jan 6, 2017 at 17:27

Comments

1

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);

Here is a live example.

answered Jan 6, 2017 at 17:31

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.