title: Avoid manual creation of lambda to wrap the call to new[]: to be used as a generator function in std::generate
body: <h2>Short Version</h2>
  <p>Currently, I manually create a lambda function for the <code>new []</code>, as follows:</p>
  <pre><code>[](){ return new double[3]; }
  </code></pre>
  <p>This lambda is later used as a generator function in the call to <a href="https://en.cppreference.com/w/cpp/algorithm/generate" rel="nofollow noreferrer"><code>std::generate</code></a>:</p>
  <pre><code>std::generate(vec.begin(), vec.end(), [](){ return new double[3]; });
  </code></pre>
  <p>Can I avoid the creation of this lambda and use some more standard functionality/syntax sugar/alternatives? C++17 is preferred; however, if C++20 (or some proposal) offers something better, I am very interested as well.</p>
  <p>The long version provides some background, and an existing solution for a similar situation, when <a href="https://en.cppreference.com/w/cpp/algorithm/for_each" rel="nofollow noreferrer"><code>for_each</code></a> is used to deallocate the memory.</p>
  <hr />
  <h2>Long Version with Background</h2>
  <h3>Need</h3>
  <ol>
  <li>Create <code>std::vector</code> that stores <code>double*</code> pointers, which point to 1-D arrays of fixed size.</li>
  <li>Pass this structure to the library function that will fill it with data.</li>
  <li>Use the filled data.</li>
  <li>Clear the allocated memory.</li>
  </ol>
  <h3>Conditions</h3>
  <ul>
  <li>Due to (2), I do not have control over the type of argument for the library function; thus, I am limited to using <code>std::vector&lt;double*&gt;</code> no matter how other containers might be better suited.</li>
  <li>Want clean, minimalistic, and efficient code</li>
  <li>In particular, I started from simple plain <code>for</code>-loops and decided to go for <a href="https://en.cppreference.com/w/cpp/algorithm" rel="nofollow noreferrer">STL <code>&lt;algorithm&gt;</code></a>.</li>
  </ul>
  <h3>Initial code</h3>
  <pre><code>#include &lt;vector&gt;
  
  int main()
  {
      // (1)
      size_t numElems = 10;
      std::vector&lt;double*&gt; vec(numElems);
      for (size_t i=0; i&lt;numElems; i++)
      {
          vec[i] = new double[3];
      }
  
      // (2)-(3) SOME ACTIVITY with vec
  
      // (4)
      for (size_t i=0; i&lt;numElems; i++)
      {
          delete [] vec[i];
      }
  }
  </code></pre>
  <p>where, for simplicity, I hardcoded the <code>numElems</code> in the vector <code>vec</code>, and used hardcoded size of the <code>double*</code> array to be <code>3</code>. The passing to external function and activity in (2)–(3) are skipped. This code works.</p>
  <h3>New code</h3>
  <p>I tried the following:</p>
  <pre><code>#include &lt;vector&gt;
  #include &lt;algorithm&gt;
  #include &lt;memory&gt;
  
  int main()
  {
      // (1)
      size_t numElems = 10;
      std::vector&lt;double*&gt; vec(numElems);
      std::generate(vec.begin(), vec.end(), [](){ return new double[3]; });
  
      // (2)-(3) SOME ACTIVITY with vec
  
      // (4)
      std::for_each(vec.begin(), vec.end(), std::default_delete&lt;double[]&gt;());
  }
  </code></pre>
  <p>which also works fine.</p>
  <h3>Question</h3>
  <p>In the revised version, the deletion is performed via <a href="https://en.cppreference.com/w/cpp/memory/default_delete" rel="nofollow noreferrer"><code>std::default_delete&lt;&gt;</code></a> applied via <a href="https://en.cppreference.com/w/cpp/algorithm/for_each" rel="nofollow noreferrer"><code>std::for_each</code></a> that conveniently provides &quot;a function representation&quot; of the language keyword <code>delete []</code> (a bit sloppy wording).</p>
  <p>Is there a similar way &quot;to represent&quot; <code>new []</code>? Or am I stuck creating the lambda manually to be used as a generator function argument for <a href="https://en.cppreference.com/w/cpp/algorithm/generate" rel="nofollow noreferrer"><code>std::generate</code></a>?</p>