title: Is it OK to make a placement new on memory managed by a smart pointer?
body: <h2>Context</h2>
  
  <p>For test purpose, I need to construct an object on non-zero memory. This could be done with:</p>
  
  <pre><code>{
      struct Type { /* IRL not empty */};
      std::array&lt;unsigned char, sizeof(Type)&gt; non_zero_memory;
      non_zero_memory.fill(0xC5);
      auto const&amp; t = *new(non_zero_memory.data()) Type;
      // t refers to a valid Type whose initialization has completed.
      t.~Type();
  }
  </code></pre>
  
  <p>Since this is tedious and made multiple times, I'd like to provide a function returning a smart pointer to such a <code>Type</code> instance. I came up with the following, but I fear undefined behavior lurk somewhere.</p>
  
  <h2>Question</h2>
  
  <p>Is the following program well defined? Especially, is the fact that a <code>std::byte[]</code> has been allocated but a <code>Type</code> of equivalent size is freed an issue?</p>
  
  <pre><code>#include &lt;cstddef&gt;
  #include &lt;memory&gt;
  #include &lt;algorithm&gt;
  
  auto non_zero_memory(std::size_t size)
  {
      constexpr std::byte non_zero = static_cast&lt;std::byte&gt;(0xC5);
  
      auto memory = std::make_unique&lt;std::byte[]&gt;(size);
      std::fill(memory.get(), memory.get()+size, non_zero);
      return memory;
  }
  
  template &lt;class T&gt;
  auto on_non_zero_memory()
  {
      auto memory = non_zero_memory(sizeof(T));
      return std::shared_ptr&lt;T&gt;(new (memory.release()) T());
  }    
  
  int main()
  {
      struct Type { unsigned value = 0; ~Type() {} }; // could be something else
      auto t = on_non_zero_memory&lt;Type&gt;();
      return t-&gt;value;
  }
  </code></pre>
  
  <p><a href="http://coliru.stacked-crooked.com/a/eba88eefafbf380a" rel="noreferrer">Live demo</a></p>