16 ms·
Syntax changes from C++11 to C++20
- cheez 6y agoGreat 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.
- tobz1000 6y agoI'm only mildly familiar with C++, but the support for template arguments in lambdas looks like an analogue for generic closures, which are absent from Rust, but would be really handy at times.
- gpderetta 6y agoNote that C++ has had generic closures since C++14, with auto parameters. Template arguments are just a refinement for that.
- verytrivial 6y agoI've been a C++ programmer for 20 years. Having just seen the example of template arguments in lambda functions: 1) I am reminded of this Larson cartoon: https://imgur.com/gallery/4gR7tX7 https://imgur.com/gallery/4gR7tX7 2) I have a palpable urge to learn Go.
- UncleOxidant 6y agoI've programmed in C++ off and on for the last 25 years. I keep wondering if I should spend the time to level up on modern C++ (C++11,14,17,20) or if I might be better off to just chuck it all and dive into Rust and/or Zig and place my bets there instead.
- speedgoose 6y agoGo with Golang if you want simplicity. Go with Rust if you want a complex but still neat programming language. Rust is much better than C++ in my humble opinion. I use these programming language as a hobby and I don't think I will ever use c++ on a new project again.
- UncleOxidant 6y agoI've never been that impressed with Go - it seems to be missing a lot of modern features. To me, Zig looks nice from a simplicity standpoint and it fits in the systems programming space.
- objektif 6y agoCan we make the beautiful C syntax insufferable? Yes sure just leave it to the committee.
- gumby 6y agoWell, these are features that don’t exist in C. Plus the complex template syntax is mostly unnecessary in C++20
- im3w1l 6y agoSo that ForwardToTestFunc right? At what time are the template arguments filled in? Could you call it first with an int and then later with a string, each call forwarding to a different overload?
- gumby 6y agoYes, that’s precisely the use case. The compiler synthesizes two functions, one for each case, then optimizes them separately.
- Animats 6y agoThe amusing thing is that C++ still doesn't have nested functions. (Although I think that was a GCC feature at one point.) As I point out now and then, there are two basic concepts here - closures and anonymous functions. A lambda is both. All four combinations (functions with/without closure, and with/without names) are possible. There's a tendency to confuse the two. An anonymous function is syntactical sugar. A closure is a run-time construct. - Python and Javascript have nested functions with inheritance, which are closures. Most garbage collected languages have that, because it's easy to implement. Python also has lambdas, which von Rossum didn't want to put in but did to shut up the crowd that wanted them. - Rust has nested functions without inheritance. A nested function can't access its outer scope. Rust also has lambdas, and they can inherit. C++ has now gone the same route. Non-GC languages need compile-time work to implement a closure, since something has to make a copy of what's inherited and worry about its lifetime. - It's possible to have anonymous functions without inheritance. The classic example is a sort key function, where a sort function is called with a function that defines the test.
- MiroF 6y agobut not all lambdas are closures
- pak9rabid 6y agoI thought the difference is more in how the runtime is structured, i.e. a closure has the ability to make use of the scope in which it's defined in, so that it can make use of variables and functions within it when it's actually called.
- MiroF 6y agoI don't understand, sorry. By your definition, not all lambdas are closures.
- nwallin 6y agoLambdas are (among other things) nested functions. The "tendency to confuse the two" is a feature, not a bug. Bob knows how to draw quadrilaterals. He uses this skill to draw rhombuses, rectangles, and parallelograms. IMHO you're arguing that Bob doesn't know how to draw rhombuses because the method he uses to draw rhombuses is a method to draw parallelograms. Am I misunderstanding you?
- choeger 6y agoOne of the worst design decisions in C++ (IMO, of course) is the template syntax. Templates are proper macros, abstractions over code in the sense of lisp. But C++ hides this fact by relying (mostly) on implicit template application and very, very, indirect syntax for template abstraction. For instance: who came up with the idea of templated lambdas? Yes, manually managed closures are very C++, and yes, templated closures are also very much C++. But now we have templated closures that capture local variables IF THEY EVER HAPPEN TO BE INSTANTIATED. What the actual cluster eff?
- valemidas 6y agoPerhaps I'm just dense today, but I'm still not seeing the problem with templated lambdas... if you could spell it out, I'd be much obliged! Also, I'd assume the closure is instantiated (and the vars captured) regardless of whether its ever called, but I could be wrong.
- gpderetta 6y agoTemplated lambdas are extremely powerful. Think for example of (recursive) visitations of etherogeneous containers.
- kazinator 6y agoCan you imagine C++ templates without template parameter deduction? What's the alternative: spelling out all the parameter values, wherever the template is used? mult<int, double>(x, add<double, double>(y, z)); C++ templates use type information to deduce parameters. Lisp macros may be expanded before any type analysis takes place. The fact that you can just write: mult(x, add(y, z)) and the language figures out the parameters for the mult and add templates from the declared types of x, y and z doesn't really have any parallel in mainstream Lisp dialects. The idiomatic solution would involve making the functions themselves generic so they dispatch on the actual types of the arguments. Anything relying on declared or inferred type would be a second-class citizen in a dynamic language anyway, where declarations are optional, and inference may be imperfect or absent.
- ping_pong 6y agoI was a C/C++ programmer from 1995 to 2012, almost 20 years. I moved on to more SaaS-friendly languages, but when I look at C++ now, it's incomprehensible to me. It's an entirely different language. I think I could get back into it, but it's been so long and the gap between where I left it and where it is now is so vast, coupled with the dearth of jobs compared to Python or Go, that it doesn't feel worth it anymore. It's weird knowing that a language that I spend so much time on is relegated to the past.
- boring_twenties 6y agoI've been out of the game for a while, so I could be way off here, but my experience during my brief (15-20 year) career was one could always get paid significantly more for doing C++ than Python. Which makes intuitive sense to me since it's a lot harder to master C++.
- deleted 6y ago[deleted]
- ping_pong 6y agoThis isn't true. Go/Python/non-C++ can get you dozens of jobs with very high total comp, well over anything for C++. A Senior Software Engineer at any FANG will get you over 400k/yr TC. Getting an equivalent job with C++ would be much harder.
- boring_twenties 6y agoI was under the impression that one would need C++ to get one of those 400k+ FAANG jobs. Obviously, I could be wrong. I've never worked at one myself, but they do hit me up from time to time, and seem to be mostly interested in my C++ experience. As I said in my disclaimer, my actual experience is very dated at this point, but a strong C++ developer could earn 400k+ even way back in 2011 in the NYC financial industry. Back then, I don't think I'd heard of any Python developers making much more than 150k. Nowadays, the people I know working in C++ in NYC finance are all making significantly more than 400k.