Overload journal #126, April 2015

Resource Management with Explicit Template Specialisations

Here’s my article from the April 2015 issue of Overload journal. It presents a simple, easy‐to‐use, generic RAII1 wrapper based on an interesting static polymorphism2 technique which I introduced in the previous post.

Resource Management with Explicit Template Specialisations

RAII is one of the most important and useful C++ idioms. RAII efficiently relieves the programmer of manual resource management and is a must for writing exception‐safe3 code. Perhaps, the most ubiquitous usage of RAII is dynamic memory management with smart pointers4, but there are a plenty of other resources for which it can be applied, notably in the world of low‐level libraries. Examples are Windows API handles, POSIX file descriptors, OpenGL primitives, and so on.

Also available in Russian.

  1. Resource Acquisition Is Initialization

  2. A form of polymorphism in programming code which is resolved at compile time (i.e. without dynamic dispatch overhead). 

  3. A set of contractual guidelines that class library implementers and clients can use when reasoning about exception handling safety in programming code (Wikipedia). 

  4. An abstract data type that simulates a pointer while providing additional features, such as automatic memory management or bounds checking (Wikipedia).