Smart replacement for 'new[]' to make_unique
<p>I am in process of replacing statements like:</p>

<pre><code>auto pszOutBuffer = new char[dwSize];

ReadDataFromHttp(pszOutBuffer, dwSize);
if(dwSize&gt;100)
    ParseHttpData(pszOutBuffer);
    ...
delete []pszOutBuffer;
</code></pre>

<p>TO:</p>

<pre><code>auto OutBufferPtr = make_unique&lt;char[]&gt;(dwSize);
auto pszOutBuffer = OutBufferPtr.get();

ReadDataFromHttp(pszOutBuffer, dwSize);
if(dwSize&gt;100)
    ParseHttpData(pszOutBuffer);
...
</code></pre>

<p>So that I get advantage of smart pointer <code>unique_ptr</code>. I would like to keep the variable <code>pszOutBuffer</code> as is so less changes in Git commits appear, and <code>unique_ptr.get()</code> isn't required to be repeated.</p>

<p>To make this simple, and less verbose to read; I <strong>thought</strong> of writing a macro <code>MAKE_UNIQUE_PTR(type,size)</code> which will be a single statement (<em>not</em> two as shown above). But such macro will not be able to have <code>unqiue_ptr</code> as well as <code>pszOutBuffer</code> as in:</p>

<pre><code>auto pszOutBuffer = MAKE_UNIQUE_PTR(char, dwSize);
</code></pre>

<p>I may think of writing a function template, but then... how to keep <code>unique_ptr</code> after function (<code>MAKE_UNIQUE_PTR</code>) returns?</p>

<p><strong>EDIT:</strong></p>

<p>With this <em>hypothetical</em> macro/function, the code would simply be:</p>

<pre><code>auto pszOutBuffer = MAKE_UNIQUE_PTR(char, dwSize);

ReadDataFromHttp(pszOutBuffer, dwSize);
if(dwSize&gt;100)
     ParseHttpData(pszOutBuffer);
...
</code></pre>

<p>With these advantages:</p>

<ul>
<li><code>unique_ptr</code> still controls the life time of buffer.</li>
<li>Raw-style pointer is still in place without chaging them to <code>unique_ptr::get()</code> calls.</li>
<li>Hence can safely delete <code>delete[]</code> calls from multiple code paths.</li>
</ul>

<p>NOTE that the very first code is the code I've in hand. No macro, no <code>unique_ptr</code>- just legacy code having <code>new</code> and <code>delete</code>. And yeah... those Hungarian notation variables.</p>