VC++ 6.0 workaround for partial specialization
Hello everyone,
I am working on a project on Visual C++ 6.0 and I need to port some code
that uses partial specialization of class templates:
#include <cstddef>
#include <iostream>
// Extracts the type of an object.
template <class T>
struct extract { typedef T type; };
// Extracts the type of an array.
template <class T, std::size_t N>
struct extract<T[N]> { typedef T type; };
// Primary template for object new.
template <class T> struct TrackNewHelper
{
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 TrackNewHelper<T[N]>
{
static T* TrackNew( T* ptr, const char* file, int line)
{
std::cout << "Tracking array allocation " << std::endl;
return ptr;
}
};
// Forwards the call to helper classes.
template <class T> typename extract<T>::type* TrackNew(
typename extract<T>::type* ptr, const char* file, int line )
{
return TrackNewHelper<T>::TrackNew( ptr, file, line );
}
#define NEW( T ) TrackNew<T>( 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;
}
I found a hint on the internet saying that class member templates can
simulate partial specialization, but unfortunately, no further information
was given. Does anyone happen to know a workaround for the code above?
--
Matthias Hofmann
Anvil-Soft, CEO
http://www.anvil-soft.com - The Creators of Toilet Tycoon
http://www.anvil-soft.de - Die Macher des Klomanagers