Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm not an expert in placement new but there are a couple of issues with how you are defining the macro. </p> <h2>Issue 1</h2> <p>The most obvious problem is the use of the cast <code>(TYPE*)STORAGE</code> for the storage location. This is incorrect. Placement new is just another C++ function and it participates in operations like overload resolution. Arbitrarily casting the memory to a specific type could cause the placement new to bind to a different operator new that the user expected. </p> <p>For example, it's valid to have the following two definitions of placement new. Your macro would potentially cause the wrong one to be called. </p> <pre><code>void * _cdecl operator new(size_t cbSize, void* pv); void * _cdecl operator new(size_t cbSize, SomeType* pv)- </code></pre> <p>...</p> <pre><code>// These two call different overloads void* p = malloc(sizeof(SomeType)); SomeType* f1 = CONSTRUCT_INPLACE(SomeType, p,()) SomeType* f2 = new (p) SomeType(); </code></pre> <p>I wrote a blog post awhile back on how you can use this type of overload resolution to implement custom allocators. </p> <ul> <li><a href="http://blogs.msdn.com/jaredpar/archive/2007/10/17/c-placement-new-and-allocators.aspx" rel="nofollow noreferrer">http://blogs.msdn.com/jaredpar/archive/2007/10/17/c-placement-new-and-allocators.aspx</a></li> </ul> <h2>Issue 2</h2> <p>The expression STORAGE in the macro should be wrapped in parens to prevent evil macro expansion bugs. </p> <pre><code>::new((TYPE*)(STORAGE)) TYPE INIT </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload