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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
template boy
  • 10,230
  • 8
  • 61
  • 97
  • 7
    No. Why would you need to do this? Just declare a normal function. – Benjamin Lindley Oct 27 '12 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. – Praetorian Oct 27 '12 at 16:04
  • Perhaps you are looking for this SO [doc link](http://stackoverflow.com/documentation/c%2b%2b/572/lambdas#t=201702110629374323036). It's quite detailed! Check it out. – NameRakes Feb 11 '17 at 06: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. – Thomas Tempelmann Sep 06 '17 at 17:40
  • Huh, found the answer: https://stackoverflow.com/questions/8109571/lambda-as-function-parameter – Thomas Tempelmann Sep 06 '17 at 17:45

1 Answers1

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.

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85