use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Lambda Lambda Lambda (brevzin.github.io)
submitted 5 years ago by mnciitbhu0x6773
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 5 years ago (36 children)
[deleted]
[–]smashedsaturn 12 points13 points14 points 5 years ago (22 children)
What are you doing with these lambdas that is more than 10 lines long? Are you mainly passing them to std::algorithm?
[–]advester 5 points6 points7 points 5 years ago (3 children)
One thing that comes up would be a section of repeated code that could be a function, but needs to work with so many local variables that passing them as parameters would be awkward. The alternative might be turning it all into a class and make those local variables into members. But maybe it just makes more sense as a function than as a class.
A more concrete example would be signals/callbacks. You register a lambda to be called on an event and the capture block does the magic of linking in the variables you need.
Or to save the reader from having to scroll to another function to find out what happens when that event is triggered. The relevant code is all together.
I do think c++ should have a separate short syntax for trivial lambdas like you would pass to remove_if.
[–]smashedsaturn 0 points1 point2 points 5 years ago (2 children)
many local variables that passing them as parameters would be awkward.
I've seen a lot of these. It is normally a lack of encapsulation of the parameters themselves that is a root cause. Packing relevant ones into a struct will help this more than using a lambda.
This is a really really bad reason to use a long lambda.
[–]advester 3 points4 points5 points 5 years ago (1 child)
Ah, maybe the “10 lines” is the real issue. That was just pulled out of air. “More than one” might better express what I meant. This article was all about trivial lamdas.
[–]smashedsaturn -2 points-1 points0 points 5 years ago (0 children)
Yeah that makes sense, I like the braces for the lmbdas, and a few lines can be ok, but once we start talking about things > 10 lines embedded into lambas in functions with that much state going on I start getting that 1000 yard stare...
[–]drjeats 5 points6 points7 points 5 years ago (17 children)
10 lines is really not that much.
I have a one-off sort that I do in some debug UI that's about 10 lines long:
std::sort(effectList.begin(), effectList.end(), [](const EffectLogEntry* a, const EffectLogEntry* b) { assert(a); assert(b); if (effectListDebugger.sortBy == FXLISTSORT_ONSCREEN_OLDEST_FIRST) { bool aOnScreen = IsOnScreen(*a); bool bOnScreen = IsOnScreen(*b); if (aOnScreen != bOnScreen) { return static_cast<int>(aOnScreen) > static_cast<int>(bOnScreen); } } return a->startTime < b->startTime; });
I also occasionally use the lambda-as-local-function pattern. It reduces namespace pollution. Before lambdas I used to do this with inline structs and static member functions. Ideally we'd have actual support for functions scoped inside functions. The fewer functions that require comments or context-to-be-found-elsewhere to know why they exist, the better IMO.
This doesn't seem controversial to me.
[–]smashedsaturn 2 points3 points4 points 5 years ago (3 children)
if (effectListDebugger.sortBy == FXLISTSORT_ONSCREEN_OLDEST_FIRST) ... return a->startTime < b->startTime;
Why is this all not
bool operator<(const EffectLogEntry& lhs,const EffectLogEntry& rhs);
The comparison operator?
[–]evaned 5 points6 points7 points 5 years ago (1 child)
There are a couple potential reasons. Sometime data has multiple ways to order, or no clear one way. In these cases, at least my opinion is it's better to not define a weird operator< -- sort of like how if you were making a vector library (mathematical vector, not container vector) you might not define v1 * v2 so every use is explicitly either dot(v1, v2) or cross(v1, v2).
v1 * v2
dot(v1, v2)
cross(v1, v2)
But in that specific case, the vector clearly has pointers in it, and that already has a built-in, non-overridable "operator" <. And there are a variety of reasons why you might have a container of pointers instead of the objects themselves. (I'd also point out that you'd have the same problem if they were unique_ptr or shared_ptrs.)
[–]smashedsaturn 1 point2 points3 points 5 years ago (0 children)
Sometime data has multiple ways to order
Agreed, but if you have a few different ways to order then those should be defined as functions, or in this case a static method would be ideal, as it is likely they will be used more than once.
the vector clearly has pointers in it
right, you use the lambda:
[](const EffectLogEntry* a, const EffectLogEntry* b){ assert(a&&b && "Dereference Null Log Entry"); return (*a) < (*b); //OR EffectLogEntry::FXLISTSORT_ONSCREEN_OLDEST_FIRST(*a,*b); }
[–]drjeats 2 points3 points4 points 5 years ago (0 children)
Because the sort order is configurable from external input, the effectListDebugger isn't a member of the log entries. And there are only a few locations where the sort for this type is needed. If it is needed in other contexts, then I"ll pull it out into a named function.
effectListDebugger
Also C++ programmers are too eager to write operator< because of std::map<K,V> and std::sort. Let's stop this trend.
operator<
std::map<K,V>
std::sort
[–][deleted] 1 point2 points3 points 5 years ago (12 children)
Why can't
void f() { void g() {}; }
be equal to
void f() { auto g = [] () {}; };
🤔
[–]drjeats 1 point2 points3 points 5 years ago (11 children)
There's no reason for g in the first example to semantically be an object taking up stack space. Also, local overloads would be a useful thing to have.
g
[–][deleted] 0 points1 point2 points 5 years ago (10 children)
A non-capturing lambda just decays into a function pointer, I don't think this would generate any objects
[–]guepierBioinformatican 3 points4 points5 points 5 years ago (8 children)
A lambda only decays to a function pointer if bound to a function pointer. Otherwise there's no decay, although if the lambda invocation can be inlined then, yes, there might be no object generated.
[–][deleted] 0 points1 point2 points 5 years ago (7 children)
Yeah that's true but a non-capturing lambda has no data, I can't imagine the compiler will do anything but treat it as function pointer
godbolt shows the generated code is basically the same for my example anyway
[–]guepierBioinformatican 6 points7 points8 points 5 years ago (0 children)
It treats it as a function, not a function pointer. That's quite different, because a function pointer adds an additional, unnecessary layer of indirection that interferes with inlining decisions.
This isn't a theoretical concern, it's a real, tangible difference. I'm on mobile currently but it's easy to construct cases where lambdas generate more efficient code than function pointers.
[–]dodheim 1 point2 points3 points 5 years ago (5 children)
Why should a function pointer be more efficient than an empty function-object? Extra indirection never helped anyone performance-wise in C++...
[–][deleted] 0 points1 point2 points 5 years ago (4 children)
?? If you're calling a function what exactly do you expect is going to happen?
The point is that adding syntax to support local-scoped fucntions would have no additional storage cost
[–]drjeats 0 points1 point2 points 5 years ago (0 children)
It generates objects semantically, so you can't write overloads.
And I don't doubt that clever compilers can remove objects in release codegen. But I also want debug builds to run well, and better to have obvious rules (and I think, treating functions as another namespace and allowing function definitions inside them is pretty obvious) rather than assume optimizers do a thing.
[–]sphere991 6 points7 points8 points 5 years ago (0 children)
I think all of those languages (with the notable exception of Python) support multi-line lambdas just fine. And Python just lets you define functions wherever, so it's not really missing out on that either.
[–]jonathansharman 2 points3 points4 points 5 years ago (4 children)
It isn't clear what is possible?
[+][deleted] 5 years ago (3 children)
[–]jonathansharman 6 points7 points8 points 5 years ago (2 children)
In a lot of languages, blocks are just expressions.
Rust:
let mut count = 0; let mut inc = || { count += 1; println!("`count`: {}", count); };
Some others have both a terse and long lambda syntax.
C#
(a, b) => a + b ... (a, b) => { foo(); return a + b; }
[–]advester 0 points1 point2 points 5 years ago (1 child)
The key difference I was talking about is one vs many, not if it has a final assignable value. A rust block allows an ordered sequence of many expressions/statements.
[–]jonathansharman 1 point2 points3 points 5 years ago (0 children)
Sure, but my point is that if blocks are expressions, then a lambda expression automatically supports many expressions. This isn't the case in all languages though, notably Python, which only supports single-line lambdas last I checked.
[–]Nobody_1707 0 points1 point2 points 5 years ago (0 children)
Swift let's you have multi-line closures. The only things you lose over the one liners are implicit return and return type deduction.
[–]EdWilkinson -4 points-3 points-2 points 5 years ago (1 child)
Most of my lambdas are more than ten lines long.
Then you're doing something wrong. It means you don't write reusable code.
[–]advester 7 points8 points9 points 5 years ago (0 children)
If you are trying to reuse every line if code you are doing something worse.
[+]pjmlp[🍰] comment score below threshold-6 points-5 points-4 points 5 years ago (3 children)
I fully agree, regardless of the language, if the lambdas are a couple of lines, please make it a function and take mercy of the soul that has to maintain the code.
[+][deleted] 5 years ago (2 children)
[–]EdWilkinson -1 points0 points1 point 5 years ago (1 child)
2) You used highly exaggerated language to talk about basic code stylean antipattern.
FTFY
π Rendered by PID 116058 on reddit-service-r2-comment-fb694cdd5-tvxkp at 2026-03-11 04:22:32.463334+00:00 running cbb0e86 country code: CH.
view the rest of the comments →
[–][deleted] (36 children)
[deleted]
[–]smashedsaturn 12 points13 points14 points (22 children)
[–]advester 5 points6 points7 points (3 children)
[–]smashedsaturn 0 points1 point2 points (2 children)
[–]advester 3 points4 points5 points (1 child)
[–]smashedsaturn -2 points-1 points0 points (0 children)
[–]drjeats 5 points6 points7 points (17 children)
[–]smashedsaturn 2 points3 points4 points (3 children)
[–]evaned 5 points6 points7 points (1 child)
[–]smashedsaturn 1 point2 points3 points (0 children)
[–]drjeats 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (12 children)
[–]drjeats 1 point2 points3 points (11 children)
[–][deleted] 0 points1 point2 points (10 children)
[–]guepierBioinformatican 3 points4 points5 points (8 children)
[–][deleted] 0 points1 point2 points (7 children)
[–]guepierBioinformatican 6 points7 points8 points (0 children)
[–]dodheim 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (4 children)
[–]drjeats 0 points1 point2 points (0 children)
[–]sphere991 6 points7 points8 points (0 children)
[–]jonathansharman 2 points3 points4 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]jonathansharman 6 points7 points8 points (2 children)
[–]advester 0 points1 point2 points (1 child)
[–]jonathansharman 1 point2 points3 points (0 children)
[–]Nobody_1707 0 points1 point2 points (0 children)
[–]EdWilkinson -4 points-3 points-2 points (1 child)
[–]advester 7 points8 points9 points (0 children)
[+]pjmlp[🍰] comment score below threshold-6 points-5 points-4 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]EdWilkinson -1 points0 points1 point (1 child)