3

I'm trying to sort an array using a lambda to use another array in the comparison function. Inside a larger function, I have this code:

std::sort(arr.begin(), arr.end(),[] (int& a, int& b)
{       
    return (*i)[a] < (*i)[b];
});   

I get the following errors in the containing function:

mcmc.cpp:139: error: expected primary-expression before ‘[’ token
mcmc.cpp:139: error: expected primary-expression before ‘]’ token
mcmc.cpp:139: error: expected primary-expression before ‘int’
mcmc.cpp:139: error: expected primary-expression before ‘int’

When compiling, I'm including the -std=c++0x option as well.

I'm confused about what's going on. For some reason, it doesn't seem to recognize my syntax as valid. I did a yum update just in case, but it still seems as if it just doesn't recognize the use of lambdas.

6
  • 1
    From your I'm including the -std=c++0x option comment, I gather you're using G++, so the question is which version of G++ exactly? Not every one supports lambdas. See gcc.gnu.org/projects/cxx0x.html (this page contains the information about C++11 compatibility per G++ version, I consult it regularly but somehow the site seems to be down right now -- try again later) Commented Mar 19, 2013 at 1:06
  • It looks like you're missing a paren after the closure argument. Commented Mar 19, 2013 at 1:08
  • @syam I have v 4.4.7 gcc installed. I thought g++ was just the cpp command to run gcc. I just did a yum update, so I should have the latest gcc. Commented Mar 19, 2013 at 1:10
  • @bchurchill where exactly? I can't seem to find it. Commented Mar 19, 2013 at 1:11
  • nope, I made a mistake. There's no syntax error. It almost compiles find with my g++ version 4.6.3 -- although you need to capture i in the closure (e.g. put it in the square brackets). Commented Mar 19, 2013 at 1:39

1 Answer 1

8

C++11 lambdas require GCC/G++ 4.5 at least, they won't work with G++ 4.4.

See http://gcc.gnu.org/projects/cxx0x.html (or, since it is down right now, the cached version).

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. Just found the relevant question on SO too. I figured CentOS 6 would have lambda support in gcc, but I guess not.
@saccharine I don't know much about CentOS but you probably could install a newer GCC version from a "testing" repository rather than the regular "stable" repository (sorry for the Debian wording ;) you get my point anyway). Eg. I have g++ 4.7 and 4.8 running on Squeeze (Debian stable, GCC 4.4 by default) with only very minimal upgrades.
is the same issue with std::addressof? I am using g++4.4 because CUDA requires it, but my program doesn't see std::addressof, I see that in 4.4 files this is not present, only in my 4.7 dirs
@cf16 it seems that your question already contains the answer. ;)

Your Answer

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.