Sometimes it is easier to represent a value by function, and lambdas are good for this. But is there any way to return value from lambda declaration?
for example:
int i = []{return 2;};
generates an error. How to make that lambda declaration return 2
without any new lines of code?
Like calling any functions using the calling operator()
, you need to call the lambda.
int i = []{return 2;}();
// ^^
Addtionaly to the answers provided. This is called an immediately-invoked function expression (IIFE), or sometimes, immediately-invoked lambda expression. (FF comes from C++ is widely used in other languages)
int i = []{return 2;}(); // () invokes the lambda AKA call operator
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With