My last post covered the importance of supplying a RAII type for every pair of balanced functions in an API. Is it possible to write a reusable generic type that can implement that pattern for developers?
We might imagine a type that calls the start function in its constructor and the stop function in its destructor. The constructor would be parameterized by the start and stop functions; let them be callable objects in the general case. The start callable object would not be needed after the constructor returns, and honestly, maybe it shouldn't be passed to the constructor. Maybe the caller of the constructor should be responsible for calling the start function first, and then just constructing the gener
For this technique, we could write a generic type, call it Stopper
, that calls an arbitrary function in its destructor.