KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I can't think of a true RAII language that also has tail call optimization in the specs, but I know many C++ implementations can do it as an implementation-specific optimization. This poses a question for those implementations that do: given that destructors are invoked at the end of a automatic variable's scope and not by a separate garbage collection routine, doesn't it violate TCO's constraint that a recursive call must be the last instruction at the end of a function? For example:- #include <iostream> class test_object { public: test_object() { std::cout << "Constructing...\n"; } ~test_object() { std::cout << "Destructing...\n"; } }; void test_function(int count); int main() { test_function(999); } void test_function(int count) { if (!count) return; test_object obj; test_function(count - 1); } "Constructing..." would be written 999 times and then "Destructing..." another 999 times. Ultimately, 999 test_object instances would be automatically allocated before the unwind. But assuming an implementation had TCO, would 1000 stack frames exist or just 1? Does the destructor after the recursive call collide with the defacto TCO implementation requirements?
Tags (comma-separated)
Save Edits
Cancel