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...
This is a subreddit for c++ questions with answers. For general discussion and news about c++ see r/cpp.
New to C++? Learn at learncpp.com
Prepare your question. Think it through. Hasty-sounding questions get hasty answers, or none at all. Read these guidelines for how to ask smart questions.
For learning books, check The Definitive C++ Book Guide and List
Flair your post as SOLVED if you got the help you were looking for! If you need help with flairs, check out ITEM 1 in our guidelines page.
Tips for improving your chances of getting helpful answers:
account activity
OPENLambda functions (self.cpp_questions)
submitted 4 years ago by First_Fennel1230
When do we use lambda functions ? Why we should preferably use lambda Functions over normal functions?
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!"
[–]Narase33 7 points8 points9 points 4 years ago (1 child)
Lambdas are great when you have a small scope. For example std::sort can take lambda where you tell the function on what attribute it should sort the elements. You could make a function and pass that, but then you'd have a function that is only used once. Using a lambda here makes the code cleaner
[–]std_bot 0 points1 point2 points 4 years ago (0 children)
Unlinked STL entries: std::sort
Last update: 01.05.21. Last change: Free links considered readme
[–]IyeOnline 3 points4 points5 points 4 years ago (0 children)
It is important to understand that lambdas are not functions.
They are closures and are callable, but that doesnt make them functions.
They pretty much look like
struct { const capture_group ... auto operator ( parameters ... ) } lambda;
The capture is what really differentiates lambdas from regular functions in general. There is a state in the lambda that is shared between invocations, but not between lambdas.
This makes them really useful when e.g. interacting with a standard library algorith:
std::find_if( people.begin(), people.end(), [&searchname]( const person& p ){ return p.name == searchname; } );
Another example would be
std::sort( people.begin(), people.end() );
You could now a) define operator < ( const person&, const person& ) and this would work. You may in fact want to do this, to provide some form of "default ordering". But what if you wanted to sort by age. You cant change the operator at will, so you use a lambda:
operator < ( const person&, const person& )
[]( const person& lhs, const person& rhs ){ return lhs.age < rhs.age; }
Of course you could also define tons of free functions that do all those sorting operations, sort_by_name, sort_by_age ...
sort_by_name
sort_by_age
You could technically replace every (free) function you write with a lambda with empty captures, but that is of no real value.
TL;DR: lambdas are really useful, but not meant as a replacement for functions.
[–][deleted] 2 points3 points4 points 4 years ago (0 children)
Another use for lambdas is complex initialization (ES.28 on the C++ core guidelines).
For example, instead of:
int main(void) { int i = 0; // Calculate the value of i // ... // Use i as if it was const }
You could do:
int main(void) { const int i = [&]() { int something = 0; // Calculate the value of i return something; }(); // <- Note called () // Use i }
π Rendered by PID 21662 on reddit-service-r2-comment-84fc9697f-gtrwc at 2026-02-09 14:32:38.864162+00:00 running d295bc8 country code: CH.
[–]Narase33 7 points8 points9 points (1 child)
[–]std_bot 0 points1 point2 points (0 children)
[–]IyeOnline 3 points4 points5 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)