Boost logo

Boost :

From: Andreas Huber (ahd6974-spamgroupstrap_at_[hidden])
Date: 2005-06-04 09:50:21

Hi Andreas

> Since we're using msvc 7.1 i decided to enable the disabled code and
> see what happened. The code didn't compile but after fixing some
> trivial compile errors I ended up with the following version:
> template<class T>
> struct heap_allocator
> {
> #if defined(_MSC_VER) && (_MSC_VER >= 1310)
> // 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>
> static yes has_op_new(U*, test<void* (*)(std::size_t),
> &U::operator new>* = 0) {return 0;} static no has_op_new(...)
> {return 0;}
> template<class U>
> static void * new_operator(U);
> static void * new_operator(yes){
> return (T::operator new)(sizeof(T));
> }
> static void * new_operator(no){
> return (operator new(sizeof(T)));
> }
> static T * invoke(){
> return static_cast<T
> *>(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
> };

For this to work, one needs a SFINAE compiler, right?

> which at least in our project compiles, links and works just fine. I
> sent the change to Robert Ramey and he said he would examine it but I
> thought I would consult the members of this list as well. Does this
> work as expected or are there any hidden gotchas?

I don't see any except for SFINAE, which is not supported by a few

I'm also interested in custom operator new support for serialization. I
faintly remember that early versions of the documentation (during
review) described a completely different type of support for classes
with operator new. I don't recall the details but I believe that one was
able to specialize a template for types with operator new. While being
slightly more cumbersome, this sort of support would work with virtually
any compiler, not just the ones that support SFINAE. It would be
interesting to hear why this kind of operator new support was
discontinued later.


Andreas Huber
When replying by private email, please remove the words spam and trap
from the address shown in the header. 

Boost list run by bdawes at, gregod at, cpdaniel at, john at