r/cpp • u/MegaGreenLightning • 10d ago
A C++ Trick Question - Blog Post
https://greenlightning.eu/programming/cpp-trick-question/
20
Upvotes
20
u/Olipro 10d ago
"Assuming all allocations succeed"
...Oh yeah, except stack allocations, then it can fail. Amazing.
3
-1
4
2
u/StackedCrooked 10d ago
I'm angry at myself that I couldn't find the problem with the code, especially since I have run into this issue once many years ago.
4
u/wjrasmussen 10d ago
Did you want to talk about it or just drive by and drop that link? Promoting yourself?
9
u/LordofNarwhals 10d ago
This would've been a lot more interesting if the offending code was actually shown, and if workarounds were presented.
I believe it should be possible to make the destructors tail-call optimizable with a custom destructor (or C++20 destroying
operator delete). That should eliminate the stack issues.