4 ms·
what would nested functions bring you that lambdas don't ?
by jcelerier 6y ago
what would nested functions bring you that lambdas don't ?
- s9w 6y agoHypothetical nested functions could just use the regular function syntax. Lambdas can be quite a different beast.
- f00zz 6y agogcc has had those (as a non-standard C extension) for a while. Too bad it never made it into the standard. https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html#Nested-Functions https://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html#Nes...
- rightbyte 6y agoToo bad it would be really nice for sort() etc.
- CyberDildonics 6y agoWhy would it make a difference? You can pass an anonymous function with two auto arguments as the comparison to sort.
- deleted 6y ago[deleted]
- rightbyte 6y agoIt wouldn't pollute the file scope namespace. You can define it in eye sight of the sort call, steal variables from the nesting functions stack etc.
- jcelerier 6y ago... but how's that different of what we have today ? std::sort(vec.begin(), vec.end(), [] (const auto& lhs, const auto& rhs) { return lhs.whatever < rhs.whatever; }); or auto sort_by_whatever = [] (const auto& lhs, const auto& rhs) { return lhs.whatever < rhs.whatever; }; std::sort(vec.begin(), vec.end(), sort_by_whatever);
- rightbyte 6y agoSorry was refering to C in that comment with no lambdas, with the GCC extension.
- TeMPOraL 6y agoA 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 ?