serialization of objects with overloaded new/delete

Hi, I'm working on an application where we wan't to serialize objects with overloaded operator new/delete. After reading at the library homepage that it isn't possible I decided to take a look at the library code and found the following code snippet in iserializer.hpp: template<class T> struct heap_allocator { #if 0 // note: this fails on msvc 7.0 and gcc 3.2 <snip> 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 }; 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 }; 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? Regards /Andreas Brinck ########################################### This message has been scanned by F-Secure Anti-Virus for Microsoft Exchange. For more information, connect to http://www.f-secure.com/

Hi Andreas [snip]
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 compilers. 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. Regards, -- Andreas Huber When replying by private email, please remove the words spam and trap from the address shown in the header.
participants (2)
-
Andreas Brinck
-
Andreas Huber