title: Is there a way to convert std::vector<const T*> to std::vector<T*> without extra allocations?
body: <p>Let's say I have a <code>Storage</code> of some <code>Object</code>s which has a method that aggregates pointers to some of the <code>Objects</code> in a vector. Like this:</p>
  
  <pre><code>class Storage
  {
  public:
     std::vector&lt;Object*&gt; aggregate_some_objects(); // non-const version
     std::vector&lt;const Object*&gt; aggregate_some_objects() const; // const version
  
  private:
     std::unordered_map&lt;size_t, Object&gt; m_objects; // data is stored 
                                                   // by-value in a non-vector container
  }
  </code></pre>
  
  <p>Generally, there is way to avoid copy-paste in implementation of const + non-const method pairs by calling one of them inside the other with the help of <code>const_cast</code>. Here however this is not possible because the return types of the methods are different. </p>
  
  <p>The most straightforward way to avoid copy-paste here would be to call <code>const</code> version from a non-<code>const</code> version and use the returned <code>std::vector&lt;const T*&gt;</code> to populate a separate <code>std::vector&lt;T*&gt;</code>. However this would lead to at least 2 heap allocation (one for each vector). I would like to avoid the allocations associated with the second vector.</p>
  
  <p>I wonder if there is way to write something like </p>
  
  <pre><code>template &lt;typename T&gt;
  std::vector&lt;T*&gt; remove_const_from_vector_of_ptrs(std::vector&lt;const T*&gt;&amp;&amp; input)
  {
     std::vector&lt;T*&gt; ret;
     // do some magic stuff here that does not involve
     // more memory allocations
     return ret;
  }
  </code></pre>
  
  <p>Thus, allowing to write</p>
  
  <pre><code>std::vector&lt;const Object*&gt; Storage::aggregate_some_objects() const
  {
     // non-trivial implementation
  }
  
  std::vector&lt;Object*&gt; Storage::aggregate_some_objects() 
  {
     auto objects = const_cast&lt;const Storage*&gt;(this)-&gt;aggregate_some_objects();
     return remove_const_from_vector_of_ptrs(std::move(objects));
  }
  </code></pre>
  
  <p>There is no 'release' method in <code>std::vector</code> (like <code>std::unique_ptr</code> for example) that allows transferring of memory ownership - and for a very good reason, so I expect that this is not possible.</p>
  
  <p>I also understand that if it were possible, it would be a dangerous operation that should be generally avoided, just as <code>const_cast</code>. But a careful usage in cases like this seems more beneficial than copy-pasting.</p>
  
  <p><strong>Edit:</strong> added clarifications to what do I mean by 'extra' allocations and changed <code>Storage::aggregate_objects()</code> to <code>Storage::aggregate_some_objects()</code> to better indicate that the implementation of these methods is more complex then a range-based loop - hence the desire to avoid copy-pasting the implementation.</p>