Comment by Joshringuk

3 months ago

Yes the temp allocator will remove things at the end of it's scope. It makes it very clear when memory will be affected and gives predictable code execution.

There are other contexts for managing mutex locks so they auto close and you could dream up one for database connections and transactions too.

The nice things about a context is you can always see it's there, destructors by design are a bit hidden which can make code harder to reason about.

Destructors take care of all these situations and because they are hidden and run automatically at the end of a scope you don't forget them or mess them up.

I have never heard anyone say they are hard to reason about, especially if there is no garbage collection or inheritance.