Boost C++ Libraries: Ticket #2520: Overloaded new operator are not called from serialization https://svn.boost.org/trac10/ticket/2520 <p> Could someone help to update the serialization code in boost to call the overloaded new operator, please? There already exist some code, but the code is not active because of a #if 0 statement. And if I set the code to #if 1 the code is not compiling with Visual C++ 2005. So I have updated the heap_allocator implementation in archive\detail\iserializer.hpp to support overload of new operator, but I'm not sure if it will work on all compilers. I have just tested the code with Visual C++ 2005. </p> <p> First the code that I used to check if the new operator is overloaded. If there are compilers that don't support the code, the implementation could be updated using defines and only return false. This way we take advantage of the information on supported compilers, an the code will work as before on unsupported compilers. Should the code that check if it has a new operator be part of the Type Traits library? </p> <pre class="wiki">typedef char True; // sizeof(True) == 1 typedef struct { char a[2]; } False; // sizeof(False) &gt; 1 template &lt;class U, U x&gt; struct test; template &lt;typename C&gt; const True *hasNewOperator(C*, test&lt;void *(*)(size_t),&amp;C::operator new&gt;* = NULL) { // Function must be implemented on // Visual C++ 2005 to prevent linking error return NULL; } template &lt;typename R&gt; const False *hasNewOperator(...) { // Function must be implemented on // Visual C++ 2005 to prevent linking error return NULL; } /* #define hasNewOperator(f) (sizeof(*hasNewOperator&lt;f&gt; (static_cast&lt;f*&gt;(NULL))) == sizeof(True)) */ </pre><p> The heap_allocator is then updated to use the hasNewOperator: </p> <pre class="wiki">template&lt;class T&gt; struct heap_allocator { template&lt;class U&gt; static T* new_operator(U); static T* new_operator(const True*) { return static_cast&lt;T *&gt;((T::operator new)(sizeof(T))); } static T* new_operator(const False*) { return static_cast&lt;T *&gt;(operator new(sizeof(T))); } static T * invoke() { return new_operator(hasNewOperator&lt;T&gt;( static_cast&lt;T *&gt;(NULL))); } }; </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/2520 Trac 1.4.3 Robert Ramey Sat, 29 Nov 2008 07:49:27 GMT status changed https://svn.boost.org/trac10/ticket/2520#comment:1 https://svn.boost.org/trac10/ticket/2520#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> Could you include a test which can be added to the test suite to verify that this works for all platforms? </p> <p> Robert Ramey </p> Ticket Runar Undheim <R.Undheim@…> Mon, 01 Dec 2008 12:21:59 GMT attachment set https://svn.boost.org/trac10/ticket/2520 https://svn.boost.org/trac10/ticket/2520 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">NewOperatorTest.cpp</span> </li> </ul> Ticket Runar Undheim <R.Undheim@…> Mon, 01 Dec 2008 12:28:45 GMT <link>https://svn.boost.org/trac10/ticket/2520#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/2520#comment:2</guid> <description> <p> I'm new to boost and to test code. But I have added a test file that I think should work. You may probably want to change some of the strings. </p> <p> Runar Undheim </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Wed, 25 Mar 2009 22:36:20 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/2520#comment:3 https://svn.boost.org/trac10/ticket/2520#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> I've implemented this idea in accordance with boost customs and practice. Testing shows that it fails on borland, sun version &lt; 5.9, IBM and pathscale. So for now, its included only for gcc and recent versions of msvc </p> Ticket