Overloading on a pointer and an array type
Hello everyone,
I am writing a memory tracker to keep track of allocations from the 'new'
and 'new[]' operators. I want to be able to distinguish between both forms,
so I am looking for code similar to the following:
#include <cstddef>
#include <iostream>
template <class T>
T* TrackAllocation( T* ptr, const char* file, int line )
{
std::cout << "Tracking object allocation." << std::endl;
return ptr;
}
template <class T, std::size_t N>
T* TrackAllocation( T (&arr)[N], const char* file, int line )
{
std::cout << "Tracking array allocation." << std::endl;
return arr;
}
#define OBJECT_NEW( T ) TrackAllocation( T, __FILE__, __LINE__ )
#define ARRAY_NEW( T ) TrackAllocation( T, __FILE__, __LINE__ )
int main()
{
// Calls TrackAllocation<int*>
// as excpected.
int* p = OBJECT_NEW( new int );
delete p;
// Calls TrackAllocation<int*>
// but should rather call
// TrackAllocation<int(&)[64]>.
p = ARRAY_NEW( new int[64] );
delete [] p;
return 0;
}
Unfortunately, the return value of the 'new[]' operator is a pointer, not an
array, so the above code does not work the way it should. One solution would
be to code like this:
template <class T>
T* TrackObjectAllocation( T* ptr, const char* file, int line )
{
std::cout << "Tracking object allocation." << std::endl;
return ptr;
}
template <class T>
T* TrackArrayAllocation( T* arr, const char* file, int line )
{
std::cout << "Tracking array allocation." << std::endl;
return arr;
}
#define OBJECT_NEW( T ) TrackObjectAllocation( T, __FILE__, __LINE__ )
#define ARRAY_NEW( T ) TrackArrayAllocation( T, __FILE__, __LINE__ )
However, I want to create a compile time error if clients of my code use the
macros the wrong way:
// ERROR: Allocating an object, but
// tracking it as an array allocation.
int* p = ARRAY_NEW( new int );
// ERROR: Allocating an array, but
// tracking it as an object allocation.
int* p = OBJECT_NEW( new int[64] );
Is there any way I can define my macros in a safer way to prevent clients
from confusing the object and array forms?
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]