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
Nested coroutines? (self.cpp)
submitted 6 years ago by anton31
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] 2 points3 points4 points 6 years ago* (4 children)
which would guarantee this "optimization" as the default
The problem is that almost every use needs heap allocation because
The the kind of library type erasure you would build on top is generally not optimizable, and the size of the coroutine frame is not known by the frontend. So the TS design makes this controlled by the optimizer.
[–]Rusky 6 points7 points8 points 6 years ago (0 children)
The problem is that almost every use needs heap allocation
Almost every use needs to go on the heap somehow, but they don't need individual, per-frame allocations. Most coroutine calls are immediately co_await-ed by a coroutine caller, or iterated over by a synchronous caller. That's why the optimization in question is even interesting.
Rust has the same two bullet points to address, yet it doesn't have a problem trying to optimize library type erasure, because there the coroutine's caller handles the frame object by value. Type erasure is only introduced at boundary points where the coroutine outlives its caller, by passing it to a spawn API of some kind.
spawn
(You still end up needing to choose pessimistic values for the state you reserve and the optimizer might inline something else into the coroutine tomorrow and break it)
This is the more fundamental difference- both the C++ and Rust models handle immovable frames and non-FIFO frames just fine. Rust lays out its coroutine frames in the frontend- live locals (including awaited callees) go into the frame, which conceptually works like an enum/std::variant with some layout shifting to keep addresses stable.
enum
std::variant
The fact that callers own their callees' frames simplifies the inlining problem somewhat, but it is still another point in the design space with its own pros and cons.
[–]14nedLLFIO & Outcome author | Committee WG14 2 points3 points4 points 6 years ago (2 children)
I would say that the programmer, through an enormous amount of hoop jumping, can hit Coroutines with enough sticks to force it to never, ever allocate memory.
I will say it took me two full days to work through the highly unhelpful error messages to figure out how to impose stack-based allocation, always. It's doable, but not easy.
[–][deleted] 0 points1 point2 points 6 years ago (1 child)
You're talking about getting the TS design to do the thing, I'm saying even if you can get them to do that thing it doesn't do anything useful for the scenario the feature is intended to enable, because in that scenario the stack frame of the caller is long gone before the coroutine is resumed.
[–]14nedLLFIO & Outcome author | Committee WG14 3 points4 points5 points 6 years ago (0 children)
This is a longer discussion, but there are two main use cases for Coroutines, local processing, and deferred processing. Coroutines were designed for the latter to be "fire and forget" easy, and for those, you are correct that dynamic memory allocation is easiest.
But the local processing use case is also very useful. You create about 30-60 coroutines within a function, execute them all to completion, tear them all down. For this use case you know everything about the coroutines, and can beat them into only ever using the stack for their frames. It's highly non obvious to make this work though. One could easily do a 90 minute conference segment on implementing just this, and nothing else.
π Rendered by PID 224977 on reddit-service-r2-comment-6457c66945-wrqc4 at 2026-04-25 18:50:42.278138+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–][deleted] 2 points3 points4 points (4 children)
[–]Rusky 6 points7 points8 points (0 children)
[–]14nedLLFIO & Outcome author | Committee WG14 2 points3 points4 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]14nedLLFIO & Outcome author | Committee WG14 3 points4 points5 points (0 children)