I’m making a class that adds some basic operations to an std::map, and I would like to automatically call delete after removing an item from the map. But if the second (T2) value isn’t a pointer, this can’t be done. Is there any way to check?
template <class T,class T2>
bool CExtendedMap<T,T2>::remove(T ID)
{
if(theMap.find(ID)!=theMap.end())
{
T2 second = theMap.find(ID)->second;
theMap.erase(theMap.find(ID));
//delete second; //Had to comment it out now.
return true;
}
return false;
}
If I understand your question correctly you’d like to behave differently if the value of the paired stored in your
CExtendedMapis or isn’t a pointer.One easy way of solving the issue is to use template overloads to get the desired effect.
Implement a wrapper function that will use
deleteif the parameter is a pointer, or do nothing if it’s not, that’s by far the easiest solution.A sample implementation is provided below: