2

I've searched SO and Google but unfortunately couldn't find an answer. I'm looking for the correct syntax to prototype a lambda. I've tried:

int g = [] () -> int;

But I get errors. Is there a way to prototype a lambda? If so, how?

5
  • 7
    No. Why would you need to do this? Just declare a normal function. Commented Oct 27, 2012 at 15:57
  • The power of a lambda is that you can define a function object that (optionally) captures variables from its context at the callsite! For other uses, you're typically better off declaring a regular function. Commented Oct 27, 2012 at 16:04
  • Perhaps you are looking for this SO doc link. It's quite detailed! Check it out. Commented Feb 11, 2017 at 6:31
  • But how do I declare a function that takes a lambda of a specific signature (e.g. one that takes one int and returns a bool)? With function pointers, I'd use typedef bool (*TheFunctionSig) (int) and use TheFunctionSig as the parameter type of my function. What's the analog with accepting lambdas? I want to be able to capture values, so I can't use a classic function ptr. Commented Sep 6, 2017 at 17:40
  • Huh, found the answer: stackoverflow.com/questions/8109571/… Commented Sep 6, 2017 at 17:45

1 Answer 1

5

You can't prototype a lambda. You can create a function object holding the lambda expression, but that wouldn't be prototyping but rather definition. E.g.: auto f = [] (int x, int y) { return x + y; }; You can also declare a standard function pointer with a type corresponding to your desired lambda signature.

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

Comments

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.