Re: Overloading on a pointer and an array type
"wijnand" <wijnandsuijlen@gmail.com> schrieb im Newsbeitrag
news:1192541622.318170.10220@z24g2000prh.googlegroups.com...
You can solve it with functors instead, because classes can have
partial template specialization.
#include <cstddef>
#include <iostream>
template <class T>
struct TrackObjectAllocation
{
T* operator()( const char *file, int line)
{
std::cout << "Tracking object allocation" << std::endl;
return new T;
}
};
template <class T, std::size_t N>
struct TrackObjectAllocation< T[N] >;
template <class T>
struct TrackArrayAllocation ;
template <class T, std::size_t N>
struct TrackArrayAllocation< T [N]>
{
T * operator()( const char *file, int line)
{
std::cout << "Tracking array allocation " << std::endl;
return new T[N];
}
};
#define OBJECT_NEW( T ) TrackObjectAllocation<T>()( __FILE__,
__LINE__ )
#define ARRAY_NEW( T ) TrackArrayAllocation<T>()( __FILE__, __LINE__)
int main()
{
int * p = OBJECT_NEW( int ); // OBJECT_NEW( int[64]) will give
compile time error
delete p;
p = ARRAY_NEW( int [64] ); // ARRAY_NEW( int ) will give a compile
time error
delete [] p;
return 0;
}
Great, this did the trick! :-) And with your idea of using classes for
partial specialization, I was even able to improve my code to provide only
one macro definition and automatically call the appropriate function for
each form of the new operator:
#include <cstddef>
#include <iostream>
// Primary template for object new.
template <class T> struct TrackNewImpl
{
static T* TrackNew( T* ptr, const char* file, int line)
{
std::cout << "Tracking object allocation" << std::endl;
return ptr;
}
};
// Partial specialization for array new.
template <class T, std::size_t N> struct TrackNewImpl<T[N]>
{
static T* TrackNew( T* ptr, const char* file, int line)
{
std::cout << "Tracking array allocation " << std::endl;
return ptr;
}
};
#define NEW( T ) TrackNewImpl<T>::TrackNew( new T, __FILE__, __LINE__ )
int main()
{
int * p = NEW( int ); // Calls the primary template.
delete p;
p = NEW( int[64] ); // Calls the partial specialization.
delete [] p;
return 0;
}
It's a pity that there is no partial specialization for function templates
though, as that would have made a much cleaner implementation possible.
Anyway, thank you very much for your solution!
--
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! ]