Re: Workaround for partial specialization wanted
"David Abrahams" <dave@boost-consulting.com> schrieb im Newsbeitrag
news:87ode7f0dd.fsf@grogan.peloton...
on Tue Nov 06 2007, "Matthias Hofmann" <hofmann-AT-anvil-soft.com> wrote:
"David Abrahams" <dave@boost-consulting.com> schrieb im Newsbeitrag
news:87hck4f0vd.fsf@grogan.peloton...
// 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; };
If your target is an old version of MSVC (6 or 7), then you can use
the Boost type traits for these kinds of things. They take advantage
of a bugfeature in the compiler that allows them to do this stuff.
I just had a look at the boost website, but all I found was the
"remove_all_extents" class template, which does not exactly offer the
behaviour as my "extract" class template.
http://www.boost.org/doc/html/boost_typetraits/reference.html#boost_typetraits.remove_extent
does.
Oh, I see. I am trying to figure out how their workaround works from the
corresponding header file:
http://boost.org/boost/type_traits/remove_extent.hpp
Does not seem to be easy, however as the above file seems to be nothing but
a master include file for quite a few other headers. Probably I will
have to
go through all of them.
Well, maybe I am trying to
achieve something that's just not possible...
It might be, depending how buggy your tools are.
I assume that getting something like boost::remove_extent to work on my
compiler is the key to the solution. Then I could maybe code as follows:
#include <iostream>
#include <boost/type_traits/remove_extent.hpp>
using boost::remove_extent;
template <class T>
T* TrackNew( T* ptr, const char* file, int line, ... )
{
std::cout << "Tracking object allocation" << std::endl;
return ptr;
};
template <class T>
remove_extent<T>::type* TrackNew( remove_extent<T>::type* ptr,
const char* file, int line, int )
{
std::cout << "Tracking array allocation" << std::endl;
return ptr;
}
#define NEW( T ) TrackNew<T>( new T, __FILE__, __LINE__, 0 )
int main()
{
int* p1 = NEW( int );
delete p1;
int* p2 = NEW( int[64] );
delete [] p2;
return 0;
}
Unfortunately, this gives me an error on my compiler, because
boost::remove_extent does not work as expected:
#include <typeinfo>
#include <iostream>
#include <boost/type_traits/remove_extent.hpp>
int main()
{
std::cout << typeid( boost::remove_extent<int[64]>::type ).name() <<
std::endl;
return 0;
}
The output I am getting for this piece of code is:
int [64]
--
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! ]