Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I understand what you want. <code>std::pair</code> has a similar feature:</p> <pre><code>std::pair&lt;T, U&gt; p(std::piecewise_construct , std::forward_as_tuple(foo, bar) , std::forward_as_tuple(qux) ); // p.first constructed in-place as if first(foo, bar) were used // p.second constructed in place as if second(qux) were used </code></pre> <p>As you can see this has a lot of benefits: exactly one <code>T</code> and <code>U</code> construction each takes place, neither <code>T</code> and <code>U</code> are required to be e.g. MoveConstructible, and this only costs the constructions of two shallow tuples. This also does perfect forwarding. As a warning though, this is considerably harder to implement without inheriting constructors, and I will use that feature to demonstrate a possible implementation of a piecewise-constructor and then attempt to make a variadic version of it.</p> <p>But first, a neat utility that always come in handy when variadic packs and tuples are involved:</p> <pre><code>template&lt;int... Indices&gt; struct indices { using next = indices&lt;Indices..., sizeof...(Indices)&gt;; }; template&lt;int Size&gt; struct build_indices { using type = typename build_indices&lt;Size - 1&gt;::type::next; }; template&lt;&gt; struct build_indices&lt;0&gt; { using type = indices&lt;&gt;; } template&lt;typename Tuple&gt; constexpr typename build_indices&lt; // Normally I'd use RemoveReference+RemoveCv, not Decay std::tuple_size&lt;typename std::decay&lt;Tuple&gt;::type&gt;::value &gt;::type make_indices() { return {}; } </code></pre> <p>So now if we have <code>using tuple_type = std::tuple&lt;int, long, double, double&gt;;</code> then <code>make_indices&lt;tuple_type&gt;()</code> yields a value of type <code>indices&lt;0, 1, 2, 3&gt;</code>.</p> <p>First, a non-variadic case of piecewise-construction:</p> <pre><code>template&lt;typename T, typename U&gt; class pair { public: // Front-end template&lt;typename Ttuple, typename Utuple&gt; pair(std::piecewise_construct_t, Ttuple&amp;&amp; ttuple, Utuple&amp;&amp; utuple) // Doesn't do any real work, but prepares the necessary information : pair(std::piecewise_construct , std::forward&lt;Ttuple&gt;(ttuple), std::forward&lt;Utuple&gt;(utuple) , make_indices&lt;Ttuple&gt;(), make_indices&lt;Utuple&gt;() ) {} private: T first; U second; // Back-end template&lt;typename Ttuple, typename Utuple, int... Tindices, int... Uindices&gt; pair(std::piecewise_construct_t , Ttuple&amp;&amp; ttuple, Utuple&amp;&amp; utuple , indices&lt;Tindices...&gt;, indices&lt;Uindices...&gt;) : first(std::get&lt;Tindices&gt;(std::forward&lt;Ttuple&gt;(ttuple))...) , second(std::get&lt;Uindices&gt;(std::forward&lt;Utuple&gt;(utuple))...) {} }; </code></pre> <p>Let's try plugging that with your mixin:</p> <pre><code>template&lt;template&lt;typename&gt; class... Mixins&gt; struct Mix: Mixins&lt;Mix&lt;Mixins...&gt;&gt;... { public: // Front-end template&lt;typename... Tuples&gt; Mix(std::piecewise_construct_t, Tuples&amp;&amp;... tuples) : Mix(typename build_indices&lt;sizeof...(Tuples)&gt;::type {} , std::piecewise_construct , std::forward_as_tuple(std::forward&lt;Tuples&gt;(tuples)...) , std::make_tuple(make_indices&lt;Tuples&gt;()...) ) { // Note: GCC rejects sizeof...(Mixins) but that can be 'fixed' // into e.g. sizeof...(Mixins&lt;int&gt;) even though I have a feeling // GCC is wrong here static_assert( sizeof...(Tuples) == sizeof...(Mixins) , "Put helpful diagnostic here" ); } private: // Back-end template&lt; typename TupleOfTuples , typename TupleOfIndices // Indices for the tuples and their respective indices , int... Indices &gt; Mix(indices&lt;Indices...&gt;, std::piecewise_construct_t , TupleOfTuples&amp;&amp; tuple, TupleOfIndices const&amp; indices) : Mixins&lt;Mix&lt;Mixins...&gt;&gt;(construct&lt;Mixins&lt;Mix&lt;Mixins...&gt;&gt;&gt;( std::get&lt;Indices&gt;(std::forward&lt;TupleOfTuples&gt;(tuple)) , std::get&lt;Indices&gt;(indices) ))... {} template&lt;typename T, typename Tuple, int... Indices&gt; static T construct(Tuple&amp;&amp; tuple, indices&lt;Indices...&gt;) { using std::get; return T(get&lt;Indices&gt;(std::forward&lt;Tuple&gt;(tuple))...); } }; </code></pre> <p>As you can see I've gone one level higher up with those tuple of tuples and tuple of indices. The reason for that is that I can't express and match a type such as <code>std::tuple&lt;indices&lt;Indices...&gt;...&gt;</code> (what's the relevant pack declared as? <code>int...... Indices</code>?) and even if I did pack expansion isn't designed to deal with multi-level pack expansion too much. You may have guessed it by now but packing it all in a tuple bundled with its indices is my modus operandi when it comes to solving this kind of things... This <em>does</em> have the drawback however that construction is not in place anymore and the <code>Mixins&lt;...&gt;</code> are now required to be MoveConstructible.</p> <p>I'd recommend adding a default constructor, too (i.e. <code>Mix() = default;</code>) because using <code>Mix&lt;A, B&gt; m(std::piecewise_construct, std::forward_as_tuple(), std::forward_as_tuple());</code> looks silly. Note that such a defaulted declaration would yield no default constructor if any of the <code>Mixin&lt;...&gt;</code> is not DefaultConstructible.</p> <p>The code has been tested with a snapshot of GCC 4.7 and works verbatim except for that <code>sizeof...(Mixins)</code> mishap.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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