A del_fun Function Adaptor for STL Containers
Copyright © 2003-2017 Wesley Steiner
This article presents a C++ template definition (del_fun) that automatically applies the C++ delete operator to polymorphic pointer elements of an STL container object when invoked via iterative STL functions such as for_each.
Under ideal conditions STL containers are designed to hold class objects (instances of C++ classes). This greatly simplifies our job as C++ programmers by having the compiler automatically take care of all object copy, assignment and cleanup that occurs as a result of the application of STL functions. Unfortunately this convenience is not available for containers of pointers and containers of pointers occur frequently in C++ when polymorphic objects are involved.
One of the first things you learn as an STL programmer is that declaring a container of polymorphic objects doesn't work, you must use a container of pointers to polymorphic objects. As an example consider the following all to familiar polymorphic class hierarchy:
Here Shape is an abstract base class of RectangleShape and CircleShape which are concrete derived classes that implement the pure virtual Draw method. In order to use these shape objects in an STL container we must define a container of pointers to the base Shape class as follows:
Then you can populate the container with concrete objects derived from this abstract base class like this:
Keep in mind when using STL containers of pointers that all of your concrete classes must be safe for copying and assignment but that's another story for another day.
For now lets assume you are using your containers of pointers and now its time to end your application and cleanup resources. As mentioned above STL containers take care of deleting resources when the container goes out of scope. In the above example the container object will automatically release the memory used by each element, a Shape*, when it goes out of scope. However the objects that the elements point to are not destroyed. Destroying the objects pointed to are your responsibility.
Early adopters of the STL library, myself included, usually solved this as follows:
Quickly we learned that writing our own loops to iterate over an STL container was reinventing the wheel and, more importantly, inviting an opportunity to introduce bugs. The STL solution is to use the for_each function as follows:
A common, and valid, complaint with this solution is that you need to write a one-line function to do the delete for every type of base object class. Once again this gets tedious and is prone to errors. In a perfect world what you really want to do is call the delete operator in place like this:
Unfortunately, as much as we would like it too, the above line of code will not compile and rightly so if you look at the definition of for_each.
Reading past the cryptic nature of STL code we can see that the for_each function iterates over the elements of the container and applies the _Op argument, via a function call, with the element as its argument. The trick used throughout STL involves the use of Function Adaptors or Functors. Functors are C++ classes that implement a function operator, operator(), to execute the body of the function. In our case the code to be executed is the call to the delete operator on the pointer value of each container element.
When developing C++ template solutions I find it is often easier to first write a solution for a specific type and then generalize it later with templates.
Lets start by writing a specialized Shape functor that we can pass to the for_each function that will do the job of calling the delete operator on the pointer argument:
Here the purpose of the del_shape function is to return a functor class, del_shape_t, which when invoked inside the for_each loop will apply the del_shape_t::operator() method on the argument. The body of del_shape_t::operator() simply calls the delete operator as desired.
Of course this example only works for containers of Shape* elements. In order to make this more useful we need to generalize it for any pointer element types by using templates.
In order to generalize the above functor for use with any pointer types we simply replace the Shape type with a template parameter as follows:
The purpose of the del_fun function is to return an instance of a del_fun_t object which is passed to the for_each function.
All we need to do now is include the pointer type as a template parameter in the call to for_each as follows:
Now we have a functor that will apply the delete operator to any pointer type. This functor does with templates what we would need to do explicitly.
The del_fun function can be used anywhere a functor argument is needed in STL. The same pattern can be adopted to invoke other C++ operators or functionality as necessary.
Scott Meyers, Effective STL: 50 Specific Ways to Improve Your Use of the Standard Template Library (Addison-Wesley, 2001)
Scott Meyers, Effective C++: Second Edition, 50 Specific Ways to Improve Your Programs and Designs (Addison-Wesley, 1998)