2

I am trying to run a simple lambda example.

// lambda.cpp #include <functional> //#include <tr1/functional> int main() { // Assign the same lambda expression to a function object. function<int (int, int)> f2 = [] (int x, int y) { return x + y; }; //function<int (int, int)> f2 = [] (int x, int y) { return x + y; }; } 

I'm compiling it like this:

$ g++ -std=c++0x -fpermissive lamdas.cpp lambdas.cpp: In function ‘int main()’: lambdas.cpp:10: error: expected primary-expression before ‘=’ token lambdas.cpp:10: error: expected primary-expression before ‘[’ token lambdas.cpp:10: error: expected primary-expression before ‘]’ token lambdas.cpp:10: error: expected primary-expression before ‘int’ lambdas.cpp:10: error: expected primary-expression before ‘int’ lambdas.cpp:10: error: expected ‘;’ before ‘{’ token 

How do I get it to compile with no errors?

8
  • 7
    What version of GNU c++ is this? I've never seen it spelling-correct source file names on the fly! Commented Jul 5, 2011 at 21:12
  • The lambda isn't the problem. You could replace it with something else, and you'd still get these errors. Commented Jul 5, 2011 at 21:12
  • @Tomalak: I think it's got style Commented Jul 5, 2011 at 21:13
  • 1
    @sehe: Hehe; they brought it in with C++0x support, guesstimating that the average human's ability to write basic words will completely vapourise after an hour with the new standard. Commented Jul 5, 2011 at 21:13
  • 1
    To be honest, I'm surprised the compiler did not substitute the equivalent ISO token Λάμβδα (or λ) in order to be totally standards conforming :) Commented Jul 5, 2011 at 21:47

3 Answers 3

5

Did you mean std::function?

Standard library features live in the std namespace.

It's also interesting that your copy/paste is clearly fake; you wrote "lamdas.cpp" then compiled "lambdas.cpp"!

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

5 Comments

He also has lambda.cpp at the top of his source file!!
@Marlon: Certainly a sign of indecision!
Also known by some as the "stood" namespace :(
@sehe: I am using GNU 4.4 version of c++.
@Sachin: Live example of the code working perfectly with std::.
2
std::function<int (int, int)> f2 = [] (int x, int y) { return x + y; }; 

or, probably better

auto f2 = [] (int x, int y) { return x + y; }; 

5 Comments

Dammit; it's past the 5 minute window in which I could have quietly stolen that good idea.
Worth remembering that the two alternatives are quite different.
Lambdas that don't capture anything are also implicitly convertible to function pointers.
@Tomalak: i tried using std::function but it doesn't seem to generate the same compile error. Agree on the file name error and the copy paste mistake as well.
@Sachin: Of course it doesn't generate the same compile error. std:: is the solution for the one you posted.
0

It looks to me like you forgot -std=c++0x.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.