Again with templates. Now I'm trying to write a Deleter
function class so that it works similar to the language one delete
. Here is my attempt:
class Deleter
{
public:
template <typename T>
void operator()(T*) const;
};
template <typename T>
void Deleter::operator()(T* ptr) const
{
std::cout << "freeing memory...\n";
delete ptr;
}
int main()
{
int* pi = new int(7);
char* cp = new char[100];
strcpy(cp, "hi there!");
Deleter del;
del(pi); // 1- argument deduced is int*
del(cp); // 2- argument deduced is char*
std::cout << "\ndone\n";
}
I've overloaded the call operator as a template thus at compile time it knows the static type of argument. (I could made it non-template replacing T
with void*
).
It works fine but I think surely that statement 2
will cause a Memory leak because I am passing a pointer to a dynamic array into delete
which works on pointers to dynamic object not array and as I've already learned it is undefined behavior to call delete on an object that had not been allocated with the corresponding new
operator.
So Is there a workaround in my code so that I can call del[]cp;
? Thank you!
You cannot use delete
with memory allocated with new[]
, and vice versa using delete[]
with memory allocated with new
. This is exactly why std::unique_ptr
and std::shared_ptr
have specializations to decide when to use delete[]
vs delete
for array and non-array memory, respectively (via the std::default_delete
template).
You are just going to have to specialize your Deleter
in a similar manner, too. Only the user of your Deleter
, not the Deleter
itself, can decide which specialization to use, since only the user has information about how the memory was allocated to begin with, and thus how it needs to be freed.
For example:
template <typename T>
class Deleter
{
public:
void operator()(T* ptr) const {
std::cout << "freeing memory using 'delete'...\n";
delete ptr;
}
};
template <typename T>
class Deleter<T[]>
{
public:
template <typename U>
void operator()(U* ptr) const {
std::cout << "freeing memory using 'delete[]'...\n";
delete[] ptr;
}
};
int main()
{
int* pi = new int(7);
char* cp = new char[100];
strcpy(cp, "hi there!");
Deleter<int> del1;
del1(pi); // uses 'delete'...
Deleter<char[]> del2;
del2(cp); // uses 'delete[]'...
std::cout << "\ndone\n";
}