
14 Sep
2012
14 Sep
'12
8:27 p.m.
On 12.09.2012 05:40, Andrew Sandoval wrote:
The first of these is RAIIFunction (I'm open to other names and coding conventions).
This approach has one problem. The typical usage is something like that: FILE* f = fopen(...); if (!f) return; RAIIFunction<void> close_f = [f](){ fclose(f); } // may throw!!! The construction of std::function for RAIIFunction may throw std::bad_alloc. In that case you will get leaked file handle. That is why approach with scope capturing reference to a base class is better.
The other one I call just RAIIWrapper, and it works with static deleter functions taking a single argument of the type being delete.
This is somewhat similar to my library RCPP. http://rain.ifmo.ru/~sorokin/rcpp/