Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to wrap std containers to store rvalue references (like unique_ptr, but on the stack)
    text
    copied!<p>I'm trying to do strange things again.</p> <p>Okay, here's the general idea. I want a <code>std::list</code> (and <code>vector</code> and so on) that actually own the objects they contain. I want to <strong>move</strong> the values into it, and access them by reference.</p> <p>Example using a list of unique_ptr:</p> <pre><code>using namespace std; list&lt;unique_ptr&lt;T&gt;&gt; items; // T is whatever type items.push_back(make_unique(...)); items.push_back(make_unique(...)); for ( unique_ptr&lt;T&gt; item : items ) item.dosomething(); </code></pre> <p>With me so far? Good. Now, let's do it with stack semantics and rvalue references. We can't just use a <code>list&lt;T&amp;&amp;&gt;</code> for obvious reasons, so we'd have to make a new class:</p> <pre><code>using namespace std; owninglist&lt;T&gt; items; items.push_back(T()); items.push_back(T()); for ( T&amp; item : items ) item.dosomething(); </code></pre> <p>Of course, I might want an <code>owningstack</code> or <code>owningvector</code> as well, so ideally we want it to be templated:</p> <pre><code>owning&lt;std::list&lt;T&gt;&gt; items; </code></pre> <p>The <code>owning&lt;U&lt;T&gt;&gt;</code> class should inherit whatever <code>push_back()</code> and <code>pop_front()</code> etc functions the underlying collection has. Presumably to achieve that, I'd need to code a generic base class, and derive explicit specialisations for the collections that have unusual functions:</p> <pre><code>template&lt;typename T&gt; owning&lt;std::queue&lt;T&gt;&gt; : owningbase&lt;T&gt; { void push_front() { ... } } </code></pre> <p>I'm getting stuck on the iterators. The <code>begin()</code> and <code>end()</code> functions should return an iterator that works the same as the underlying collection's iterator would, except with an <code>operator*()</code> that returns the item by <strong>lvalue</strong> reference instead of by value.</p> <p>We'd need some way to transfer ownership of an item out of the list again. Perhaps the iterator could have an <code>operator~</code> that returns the item as an <strong>rvalue</strong>, deletes the item from the list, and invalidates the iterator?</p> <p>Of course, all this is assuming the underlying <code>std::list</code> (or whatever) can be convinced to take an rvalue. If <code>push_back()</code> copies the value in as an lvalue, then none of this is going to work. Would I be better off coding a container from scratch? If I did, is there some way to put the majority of the code for <code>list</code>, <code>queue</code>, <code>stack</code> and <code>vector</code> into a single base class, to save rewriting pretty much the same class four times over?</p> <p>Perhaps I could introduce an intermediate class, some kind of wrapper? So <code>owned&lt;list&lt;T&gt;&gt;</code> could inherit from <code>list&lt;refwrapper&lt;T&gt;&gt;</code> or something? I know boost has a <code>reference_wrapper</code>, but I'm not sure it fits this scenario.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload