Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following is taken from the talk <a href="https://www.youtube.com/watch?v=_zgq6_zFNGY" rel="noreferrer">"Variadic Templates are Funadic"</a> by Andrei Alexandrescu at GoingNative 2012. I can recommend it for a good introduction on variadic templates.</p> <hr> <p>There are two things one can do with a variadic pack. It's possible to apply <code>sizeof...(vs)</code> to get the number of elements and expand it.</p> <h1>Expansion rules</h1> <pre><code>Use Expansion Ts... T1, ..., Tn Ts&amp;&amp;... T1&amp;&amp;, ..., Tn&amp;&amp; x&lt;Ts,Y&gt;::z... x&lt;T1,Y&gt;::z, ..., x&lt;Tn,Y&gt;::z x&lt;Ts&amp;,Us&gt;... x&lt;T1&amp;,U1&gt;, ..., x&lt;Tn&amp;,Un&gt; func(5,vs)... func(5,v1), ..., func(5,vn) </code></pre> <p>Expansion proceeds inwards outwards. When expanding two lists in lock-step, they have to have the same size.</p> <h3>More examples:</h3> <pre><code>gun(A&lt;Ts...&gt;::hun(vs)...); </code></pre> <p>Expands all <code>Ts</code> in the template argument list of <code>A</code> and then the function <code>hun</code> gets expanded with all <code>vs</code>.</p> <pre><code>gun(A&lt;Ts...&gt;::hun(vs...)); </code></pre> <p>Expands all <code>Ts</code> in the template argument list of <code>A</code> and all <code>vs</code> as the function arguments for <code>hun</code>.</p> <pre><code>gun(A&lt;Ts&gt;::hun(vs)...); </code></pre> <p>Expands the function <code>hun</code> with <code>Ts</code> and <code>vs</code> in lock-step.</p> <h3>Note:</h3> <p><code>Ts</code> is not a type and <code>vs</code> is not a value! They are aliases for a list of types/values. Either list may be potentially empty. Both obey only specific actions. So the following is not possible:</p> <pre><code>typedef Ts MyList; // error! Ts var; // error! auto copy = vs; // error! </code></pre> <h1>Expansion loci</h1> <h3>Function arguments</h3> <pre><code>template &lt;typename... Ts&gt; void fun(Ts... vs) </code></pre> <h3>Initializer lists</h3> <pre><code>any a[] = { vs... }; </code></pre> <h3>Base specifiers</h3> <pre><code>template &lt;typename... Ts&gt; struct C : Ts... {}; template &lt;typename... Ts&gt; struct D : Box&lt;Ts&gt;... { /**/ }; </code></pre> <h3>Member initializer lists</h3> <pre><code>// Inside struct D template &lt;typename... Us&gt; D(Us... vs) : Box&lt;Ts&gt;(vs)... {} </code></pre> <h3>Tempate argument lists</h3> <pre><code>std::map&lt;Ts...&gt; m; </code></pre> <p>Will only compile if there is a possible match for the arguments.</p> <h3>Capture lists</h3> <pre><code>template &lt;class... Ts&gt; void fun(Ts... vs) { auto g = [&amp;vs...] { return gun(vs...); } g(); } </code></pre> <h3>Attribute lists</h3> <pre><code>struct [[ Ts... ]] IAmFromTheFuture {}; </code></pre> <p>It is in the specification, but there is no attribute that can be expressed as a type, yet.</p>
    singulars
    1. This table or related slice is empty.
    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