I have a template class (Node is an inner class within a BST). It’s now time to free up the memory; Given that either the key or the value (or both) may be pointers, I must figure out how to free them if they are.
See an example:
~Node( void )
{
if ( is_pointer< TValue >( Value ) )
{
delete Value;
Value = NULL;
}
if ( is_pointer< TComparable >( Key ) )
{
delete Key;
Key= NULL;
}
}
The implementation behind the is_pointer< T > function works (Taken from here), however as soon as I press delete on either Key or Value, I get the following:
Error 13 error C2440: 'delete' : cannot convert from 'esc::Shader' to 'void *' c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 131
Error 14 error C2440: 'delete' : cannot convert from 'esc::ShaderComparable' to 'void *' c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 137
Error 12 error C2679: binary '=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\programming\c++\git\escalator\engine\engine\searchtree.hpp 130
I’ve tried static_cast, dynamic_cast, reinterpret_cast, etc, but neither of these appear to work.
What’s a good solution?
Don’t. Really- don’t. That’s the user’s problem- and he can supply a smart pointer if he wants this behaviour. After all, what if I want to map non-owning pointers? Or need a custom deleter?
Also, your code does not work because you compile the dead code
ifbranches anyway, because you did not use a specialization.