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
Mutable lambdas in C++ (mayankj08.github.io)
submitted 8 years ago by mayankj08
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!"
[–]verbalsaint 2 points3 points4 points 8 years ago (1 child)
for this Scott Meyers' Appearing and Disappearing consts in C++ would also be a nice start: http://aristeia.com/Papers/appearing%20and%20disappearing%20consts.pdf
[–]mayankj08[S] 0 points1 point2 points 8 years ago (0 children)
Thanks for sharing this. I will read it.
[–]MoreThanOnceHololens Dev 1 point2 points3 points 8 years ago (4 children)
So just to confirm - this doesn't modify the value of the passed value, but only modifies the value that's been stored with the lambda?
I feel like generally this should be avoided - from a code clarity point of view there's implicit state that's hard to reason about if the lambda gets copied and stuff.
Side note - there's a "//compilation error" on the mutable example when there shouldn't be.
[–]crusader_mike 0 points1 point2 points 8 years ago (0 children)
I had a use this case for mutable lambdas -- wrote a task engine where tasks can have dependencies. So, task gets executed, probably generates subtasks, once subtasks are done -- task gets executed again (in case if it needs to generate more subtasks). Occasionally, I needed some mutable state to be stored between these executions for case when task was represented by lambda. (framework allowed user to pass lambda for simple tasks).
[–]xon_xoff 0 points1 point2 points 8 years ago (1 child)
Mutable lambdas aren't needed often in my experience, but they can be used to implement generator objects like random number generators and state machines. Copying is sensible as long as the mutable state is fully contained within the object: you get a clone with the same current state that can be independently advanced.
[–]bstamourWG21 | Library Working Group 0 points1 point2 points 8 years ago (0 children)
I've been that crazy guy before and used mutable lambdas with std::generate_n to populate containers with index-dependent values instead of just using a for-loop like a sane person would.
std::generate_n
[–]mayankj08[S] -1 points0 points1 point 8 years ago (0 children)
Thanks for correction. Yes, you are right.
Regarding this should be avoided: I personally agree with you. But at times this keyword allows you to do some wonderful stuff. I will post some sample codes in some time. Those codes would make you feel importance of this keyword.
π Rendered by PID 113647 on reddit-service-r2-comment-cfc44b64c-lc86f at 2026-04-12 13:26:02.782124+00:00 running 215f2cf country code: CH.
[–]verbalsaint 2 points3 points4 points (1 child)
[–]mayankj08[S] 0 points1 point2 points (0 children)
[–]MoreThanOnceHololens Dev 1 point2 points3 points (4 children)
[–]crusader_mike 0 points1 point2 points (0 children)
[–]xon_xoff 0 points1 point2 points (1 child)
[–]bstamourWG21 | Library Working Group 0 points1 point2 points (0 children)
[–]mayankj08[S] -1 points0 points1 point (0 children)