All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TfDeleter Struct Reference

Function object for deleting any pointer. More...

Public Member Functions

template<class T >
void operator() (T *t) const
 
template<class T1 , class T2 >
void operator() (std::pair< T1, T2 * > p) const
 

Detailed Description

Function object for deleting any pointer.

An STL collection of pointers does not automatically delete each pointer when the collection itself is destroyed. Instead of writing

* for (list<Otter*>::iterator i = otters.begin(); i != otters.end(); ++i)
* delete *i;
*

you can use TfDeleter and simply write

* #include <algorithm>
*
* for_each(otters.begin(), otters.end(), TfDeleter());
*
Note
TfDeleter calls the non-array version of delete. Don't use TfDeleter if you allocated your space using new[] (and consider using a vector<> in place of a built-in array). Also, note that you need to put parenthesis after TfDeleter in the call to for_each().

Finally, TfDeleter also works for map-like collections. Note that this works as follows: if TfDeleter is handed a datatype of type std::pair<T1,T2*>, then the second element of the pair is deleted, but the first (whether or not it is a pointer) is left alone. In other words, if you give TfDeleter() a pair of pointers, it only deletes the second, but never the first. This is the desired behavior for maps.

Definition at line 115 of file tf.h.


The documentation for this struct was generated from the following file: