r/ProgrammingLanguages • • Jan 18 '26

Why not tail recursion?

In the perennial discussions of recursion in various subreddits, people often point out that it can be dangerous if your language doesn't support tail recursion and you blow up your stack. As an FP guy, I'm used to tail recursion being the norm. So for languages that don't support it, what are the reasons? Does it introduce problems? Difficult to implement? Philosophical reasons? Interact badly with other feathers?

Why is it not more widely used in other than FP languages?

76 Upvotes

117 comments sorted by

View all comments

Show parent comment

4

u/chibuku_chauya Jan 18 '26

GCC, Clang, and MSVC do TCO when optimisation is turned on (for GCC and Clang only: -O3; for MSVC only: /O2 or /Ox; or for GCC only: -foptimize-sibling-calls under all speed optimisation levels).

2

u/ggchappell Jan 19 '26

Really. What does that mean for C++? Is the last destructor call treated as a tail call? Or do they do some kind of weird magic that allows the last call in a function body to be a tail call, with destructors still happening somehow?

4

u/yuri-kilochek Jan 19 '26

I imagine it simply does nothing when there are non-trivial destructors.