5 ms·
Great survey but why on earth does there need to be a book about lambdas in C++???
by cheez 6y ago
Great survey but why on earth does there need to be a book about lambdas in C++???
- rightbyte 6y agoI just wish there was nested functions instead of these lambda classes.
- cheez 6y agoeh.... having to deal with Python's stupid lambdas, C++ lambdas are far superior. You can always do: auto func = [](...){....} for nested funcs
- deleted 6y ago[deleted]
- f00zz 6y agoYeah, using lambdas as nested functions is pretty common.
- rightbyte 6y agoYe but I would just want the possibility for the simpler syntax like the GCC extension for C where you can do: typedef void fp_t(); void foo(fp_t fp) { fp(); } int square(int num) { void square_nested() { num = num * num; } foo(square_nested); return num; } That would work as long as you don't need copy capture.
- cheez 6y agoI really don't see how that is so much better than the alternative that it would warrant a language change: int square(int num) { auto nested = [&num](){ num *= num; }; nested(num); return num; }
- rightbyte 6y agoI agree, given the fact that lambdas are already here.
- cmeacham98 6y agoSlight mistake: should be auto nested = [](int& num){ (or switch calling nested to have no params)
- cheez 6y agoThe HN C++ compiler ;-)
- jcelerier 6y agowhat 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.
- xscott 6y agoI sympathize with you here. I kind of like the lambda syntax for simple cases in C++, but the complex cases look painful to me, and based on everything else in C++, I predict there are many special cases to remember. However, if you just want a more conventional syntax for nested functions, you can almost get what you want by placing a static method inside of a local class, and it's available in older versions of C++ too: #include <stdio.h> int main() { struct foo { static void bar() { printf("nested\n"); } }; foo::bar(); return 0; } If you want it to "close" over your local variables, you'll probably need to actually instantiate an instance of the local class and use a non-static method. Doing this, the capture lists in lambdas almost start to make sense.
- rightbyte 6y agoClever. If you let the nesting function have static variables they can be referenced from the nested function too, but actually using lambdas would of course be more clear in practice. The only feature of lambdas that a proper nested function would miss is the "copy capture".
- xscott 6y ago> The only feature of lambdas that a proper nested function would miss is the "copy capture". Yeah, reference by default would fit with what people probably expect from other languages, but without a garbage collector (or similar) that's only valid for downward passed functions. Copying allows your nested function to escape the lifetime of the calling function, which might require some other/additional syntax.