Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Constructor / Destructor pairs are how you do this in C++. This is so fundamental to the object system that it's a bit steep to claim that "you've never heard of it". You have. The author's example of "get the rake - use rake - put away rake" maps exactly to [construct - call methods - destruct].

Given the jmp-iness of exception handling in C++, this is the recommended (i.e. the only safe) way to clean up after yourself.



There's even a commonly used name for it: Resource Acquisition is Initialization (RAII). It's been around for many years, and is fundamental to modern C++ style. It's beyond steep to claim we've never heard of it, just because some language has a feature that helps make it easy. Python has "with," Ruby has blocks, many languages have higher-order functions. Not an uncommon concept.


This is just for resource allocation.

The pattern the article talks about is FAR more general -- it's about all reversible processes, even things that have nothing to do with allocation.


> This is just for resource allocation.

It's the same thing, you can represent the pair (operation, inverse) as a "resource". Python calls it "context management" (and context managers), the means are slightly different, the result's the same.

I agree that OP talks about something of a wider scope though, the language features described in these sub-threads are specialized solutions to precise issues, they're not generalized reversal of arbitrary operations.


Sentry classes, which are a generalization of RAII, are used idiomatically in C++ for non-allocation tasks, such as whitespace skipping, logging, and profiling.


Also mapping state to scope e.g. mutex objects that set/clear on ctor/dtor


We can add code to destructors to do general stuff, not only deallocation. The same applies for constructors.

In C++ this works amazingly well for objects in the stack (not in the heap, as you need call the destructor yourself).




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: