4 ms·
New C++ features – lambdas
- msie 11y agoThis is slightly tangential...I was trying to learn how to debug rust code with gdb/lldb and ran into so many problems. Then just for kicks I wrote a little C++ program and used gdb - so much better :D . I just hope they get the rust debugging situation smoothed out someday but for now I won't be using it.
- brinker 11y agoDid you try using rust-gdb or rust-lldb? They're wrappers for gdb and lldb respectively that make working with Rust code a lot nicer. They ship with the Rust compiler.
- msie 11y agoYes. I couldn't single step through a simple program without delving into assembly code. Couldn't list the program. Breakpoints weren't working. I think part of the problem was working on OSX. I was reading some Rust issue related to the debug info generation. Although the issue was closed I think it's still a problem. Oh well.
- steveklabnik 11y ago> Although the issue was closed I think it's still a problem Please leave a comment if this is true! Opening old issues that come back is good.
- xigency 11y agoThis is entirely a personal opinion, and probably something I will be lambasted for, but any example using a lambda in C++ I find to be 10,000× worse than the functional equivalent. The benefit of writing code in C or C++ is that it gives your programs structure, and without naming your predicates or organizing your program in a logical way, it's basically a soup of abstract symbols. My 2¢. Kudos to the author for explaining how to use them. I naïvely assumed that the keyword `lambda' was involved.
- ska 11y ago...I find to be 10,000x worse than the functional equivalent. ironic.
- xigency 11y ago| Using a named function rather than an inline, anonymous function. Would you prefer 'the procedural equivalent'? 'Named equivalent'?
- ska 11y agoProcedural or imperative would be fine. "Functional" implies functions as first class objects, which is exactly what is being demonstrated. More to the point, your real complaint is that you only know one way to "give your programs structures". This is absolutely not a problem of functional style, but of your own understanding. A separate issue is how successful functional styles are in c++, but they have been there for ages (c.f. the STL). It's better than ok to be confused by new things, it's a great place to learn from.
- xigency 11y ago* Gives a program structure It's entirely possible for functions to be first-class while also requiring them to be named. Also don't get confused into thinking that C++ does have functions as first-class values, because it does not.
- AnimalMuppet 11y agoWhat is your definition of "first-class values" that C++ functions don't fit?
- xigency 11y agoA language that supports creating functions (and function closures) at runtime. Lambdas are essentially syntactic sugar. This requires a whole different runtime paradigm. If there were examples of code that compiles doing this, which there might be, it would probably be analogous to this: int* getVariable() { int x; return &x; } Implementing functions as first-class types generally requires a garbage collector or at minimum a different mechanic than just an ordinary stack and heap. Otherwise, your stack would start to look like swiss-cheese in any moderately functional program. Trying to write something simple, like the Y combinator, leads to a mess like this in C++14: ([](auto f) { return [](int k) { return ([f](auto g, int i) { return f([g](int j) { return g(g, j); }, i); })(([f](auto g, int i) { return f([g](int j) { return g(g, j); }, i); }), k); }; }) While you can find workable examples with template coding (and lambdas), you still won't be able to create code like you would in JavaScript for event handlers.
- santaclaus 11y agoCan you really call lambdas new at this point? C++11 has been around for what, four years now?
- arne_mertz 11y agoCompared to 17 years of standardization and >30 years of C++ altogether, 4 years is relatively new. In addition, much of the blog is not so much targeted at up-to-date C++ connoisseurs, as it is targeted at the kind of programmers I worked with in the past: people that have been stuck with legacy C++ code and older compilers for decades. So my whole "new C++ features" category covers C++11 and beyond.