Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>using some template magic, your problems can be solved by variadic template and template recursion. </p> <pre><code>template&lt; size_t N, typename T, typename U, typename... Ts &gt; struct Rows : public Rows&lt;N-1, Ts...&gt; { vector&lt;T&gt; row; U rowinfo; }; template&lt;typename T, typename U&gt; struct Rows&lt;1, T, U&gt; { vector&lt;T&gt; row; U rowinfo; }; Rows&lt; 3, int, string, float, string, double, rss_feed &gt; rows; =&gt; struct Rows { vector&lt;int&gt; v1; string s1; vector&lt;foat&gt; v2; string s2; vector&lt;double&gt; v3; rss_feed feed; } </code></pre> <p>to access the value of those field, you can implement a templated <code>get()</code> member function</p> <pre><code>template&lt; size_t N, typename T, typename U, typename... Ts &gt; struct Rows : public Rows&lt;N-1, Ts...&gt; { vector&lt;T&gt; row; U rowinfo; template&lt; size_t M &gt; typename enable_if&lt; M == N, tuple&lt;vector&lt;T&gt;&amp;, U&amp;&gt; &gt;::type get() { return make_tuple( ref(row), ref(rowinfo) ); } }; template&lt;typename T, typename U&gt; struct Rows&lt;1, T, U&gt; { vector&lt;T&gt; row; U rowinfo; template&lt; size_t M &gt; typename enable_if&lt; M == 1, tuple&lt;vector&lt;T&gt;&amp;, U&amp;&gt; &gt;::type get() { return make_tuple( ref(row), ref(rowinfo) ); } }; </code></pre> <p>take a moment and digest those. The <code>get()</code> methods return a tuple that contains reference to the request fields, and they are only enabled if the passed in number match with the class's template, what about other numbers? well we can enable them like this</p> <pre><code>template&lt; size_t M &gt; typename enable_if&lt; M != N, tuple&lt;vector&lt;T&gt;&amp;, U&amp;&gt; &gt;::type get() { return Rows&lt;N-1, Ts...&gt;::template get&lt;M&gt;(); // call parent's get, // ::template is required to avoid ambiguity } </code></pre> <p>but this is wrong!!! The return type is not <code>tuple&lt;vector&lt;T&gt;&amp;, U&amp;&gt;</code>, it should be <code>tuple&lt;vector&lt;A&gt;&amp;, B&amp;&gt;</code> where <code>A</code> and <code>B</code> are the corresponding types in the variadic template <code>Ts...</code></p> <p>But how the hell do we get the types we want from <code>Ts...</code>, well we can find out the types of a variadic template with the following technique.</p> <pre><code>template&lt; size_t N, typename... Ts &gt; struct variadic_type; template&lt; typename T, typename U, typename... Ts &gt; struct variadic_type&lt; 0, T, U, Ts... &gt; { typedef T T_type; typedef U U_type; }; template&lt; size_t k, typename T, typename U, typename... Ts &gt; struct variadic_type&lt; k, T, U, Ts... &gt; { typedef typename variadic_type&lt; k-1, Ts... &gt;::T_type T_type; typedef typename variadic_type&lt; k-1, Ts... &gt;::U_type U_type; }; </code></pre> <p>so <code>variadic_type&lt; 1, int, int, short, short, float, float &gt;::T_type</code> and <code>U_type</code> would be <code>short</code> and <code>short</code>.</p> <p>Now we have a way to find out the types of a specific location on the variadic template, we can apply it to our wrong <code>get()</code> method to get the correct return type...</p> <pre><code>template&lt; size_t M &gt; typename enable_if&lt; M != N, tuple&lt; vector&lt;typename variadic_type&lt; N-M, T, U, Ts...&gt;::T_type&gt;&amp;, typename variadic_type&lt; N-M, T, U, Ts...&gt;::U_type&amp; &gt; &gt;::type get() { return Rows&lt;N-1, Ts...&gt;::template get&lt;M&gt;(); } </code></pre> <p>We are done, we can call <code>get()</code> like</p> <pre><code>Rows&lt; 3, int, string, double, string, float, string &gt; rows; auto r3 = rows.get&lt;3&gt;(); // { vector&lt;int&gt;&amp; v, string&amp; s }; auto r2 = rows.get&lt;2&gt;(); // { vector&lt;double&gt;&amp; v, string&amp; s }; auto r1 = rows.get&lt;1&gt;(); // { vector&lt;float&gt;&amp; v, string&amp; s }; auto r4 = rows.get&lt;4&gt;(); // error:: other numbers will not compile!! </code></pre> <p>now the index rule is not very pleasant to deal with, but I think this gets the idea across.</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. 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