3 ms·
A slightly saner syntax, compared to: const auto iAmANestedFun = [stuff](stuff) -> stuff { ... } Particularly, if we could assume that nested functions auto
by TeMPOraL 6y ago
A slightly saner syntax, compared to:
const auto iAmANestedFun = [stuff](stuff) -> stuff { ... }
Particularly, if we could assume that nested functions automatically and properly capture the arguments to the parent function.
- noobermin 6y agoNot sure I like implicit capture more than explicit capture but there's [=] if you need it. Also, the return value as the article mentions is optional, so most lambdas look like const auto nestedfunc = [=](a,b) { /* */ }; Which isn't that much different to me than { /* some scope */ function nestedfunc(a,b) { /* */ } } const and auto are needed but because it's C++. Beyond that, you have an equals sign which is needed here but [=] which is three characters. Again, small differences, not sure why it looks that much worse.
- rightbyte 6y agoNow that we know lambdas they are fine, but to answer the question "why is there a book" in the beginning of the thread, it is more or less because it is not as obvious as gcc's c extension.
- CyberDildonics 6y agoWhat is so cryptic that there needs to be a separate syntax change? auto triple = [](auto x){ return 3 * x; };
- rightbyte 6y agoYe well nothing when we have already are to lambdas.
- jcelerier 6y ago> Particularly, if we could assume that nested functions automatically and properly capture the arguments to the parent function. well, how would you do that ?
- doteka 6y agoI don’t know any other language with lambdas which requires me to spell out explicitly which variables it should close over... so you tell me how Java, Scala, Clojure, Kotlin, Python, JavaScript, Rust, Go, C# and tens of others managed this clearly impossible feat of engineering.
- gpderetta 6y agoForced heap allocation and GC of course that would be unacceptable in C++. IIRC Rust uses capture modifiers similar to C++.
- steveklabnik 6y agoRust is not really the same as C++ here, you get "the compiler infers how to capture things" as the default behavior, and "take everything by value", which happens with the "move" keyword. We've found that these two cases cover the vast, vast, vast majority of usage, and so C++-style specific capture modifiers aren't necessary. You can emulate C++ style stuff by taking references outside of the closure and using move to capture only the reference.
- DCoder 6y ago> I don’t know any other language with lambdas which requires me to spell out explicitly which variables it should close over PHP closures have to specify which variables they close over, and whether it's done by-val or by-ref [0]. Nowadays there's also a shorthand single-expression-closure syntax that closes implicitly and by value [1]. [0]: https://www.php.net/manual/en/functions.anonymous.php#example-167 https://www.php.net/manual/en/functions.anonymous.php#exampl... [1]: https://www.php.net/manual/en/functions.arrow.php https://www.php.net/manual/en/functions.arrow.php
- TeMPOraL 6y agoBy having the compiler generate the equivalent of [&args...], where args are names of arguments in the parent function's header.
- jfkebwjsbx 6y agoNo need to spell each variable nor give a return type: auto f = [&](...) { ... }; That covers the majority of cases.