|
Boost Users : |
Subject: [Boost-users] Overloading the new and delete operators
From: Jacob Holm (jacobholm_at_[hidden])
Date: 2008-11-03 07:34:03
I have overloaded the new and delete operator, then I found this in the
source code:
// note: this should really be a member of the load_ptr function
// below but some compilers still complain about this.
template<class T>
struct heap_allocator
{
#if 0
// note: this fails on msvc 7.0 and gcc 3.2
template <class U, U x> struct test;
typedef char* yes;
typedef int* no;
template <class U>
yes has_op_new(U*, test<void* (*)(std::size_t), &U::operator new>* = 0);
no has_op_new(...);
template<class U>
T * new_operator(U);
T * new_operator(yes){
return (T::operator new)(sizeof(T));
}
T * new_operator(no){
return static_cast<T *>(operator new(sizeof(T)));
}
static T * invoke(){
return new_operator(has_op_new(static_cast<T *>(NULL)));
}
#else
// while this doesn't handle operator new overload for class T
static T * invoke(){
return static_cast<T *>(operator new(sizeof(T)));
}
#endif
};
If I am using a msvc that this does not fail on, is there anything wrong
with implementing this code, or will I get a bunch of errors or "hidden"
bugs? Also, are there any plans on implementing this in a future release(I
am guessing it is a work in progress issue)?
Thanks in advance.
/Jacob Holm
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net