title: May placement `new` rely on underlying storage value?
body: <p><em>Let's start with some context.</em></p>
  
  <p>A custom memory pool was using code similar to the following:</p>
  
  <pre><code>struct FastInitialization {};
  
  template &lt;typename T&gt;
  T* create() {
      static FastInitialization const F = {};
  
      void* ptr = malloc(sizeof(T));
      memset(ptr, 0, sizeof(T));
      new (ptr) T(F);
      return reinterpret_cast&lt;T*&gt;(ptr);
  }
  </code></pre>
  
  <p>The idea is that when called with <code>FastInitialization</code>, a constructor could assume that the storage is already zero-initialized and therefore only initialize those members who need a different value.</p>
  
  <p>GCC (6.2 and 6.3, at least) however has an "interesting" optimization which kicks in.</p>
  
  <pre><code>struct Memset {
      Memset(FastInitialization) { memset(this, 0, sizeof(Memset)); }
  
      double mDouble;
      unsigned mUnsigned;
  };
  
  Memset* make_memset() {
      return create&lt;Memset&gt;();
  }
  </code></pre>
  
  <p>Compiles down to:</p>
  
  <pre><code>make_memset():
          sub     rsp, 8
          mov     edi, 16
          call    malloc
          mov     QWORD PTR [rax], 0
          mov     QWORD PTR [rax+8], 0
          add     rsp, 8
          ret
  </code></pre>
  
  <p>But:</p>
  
  <pre><code>struct DerivedMemset: Memset {
      DerivedMemset(FastInitialization f): Memset(f) {}
  
      double mOther;
      double mYam;
  };
  
  DerivedMemset* make_derived_memset() {
      return create&lt;DerivedMemset&gt;();
  }
  </code></pre>
  
  <p>Compiles down to:</p>
  
  <pre><code>make_derived_memset():
          sub     rsp, 8
          mov     edi, 32
          call    malloc
          mov     QWORD PTR [rax], 0
          mov     QWORD PTR [rax+8], 0
          add     rsp, 8
          ret
  </code></pre>
  
  <p>That is, only the first 16 bytes of the <code>struct</code>, the part corresponding to its base, have been initialized. Debugging information confirm that the call to <code>memset(ptr, 0, sizeof(T));</code> has been completely elided.</p>
  
  <p>On the other hand, both ICC and Clang both call <code>memset</code> on the full size, here is Clang's result:</p>
  
  <pre><code>make_derived_memset():               # @make_derived_memset()
          push    rax
          mov     edi, 32
          call    malloc
          xorps   xmm0, xmm0
          movups  xmmword ptr [rax + 16], xmm0
          movups  xmmword ptr [rax], xmm0
          pop     rcx
          ret
  </code></pre>
  
  <p>So the behavior of GCC and Clang differ, and the question becomes: is GCC right and producing better assembly, or is Clang right and GCC buggy?</p>
  
  <hr>
  
  <p>Or, in terms of language lawyering:</p>
  
  <p><strong>Under which circumstances may a constructor rely on the previous value stored in its allocated storage?</strong></p>
  
  <p>Note: I assume this only matters with placement <code>new</code>, but I am happy to be shown otherwise.</p>