<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="FeedCreator 1.8" -->
<?xml-stylesheet href="https://baszerr.eu/lib/exe/css.php?s=feed" type="text/css"?>
<rss version="2.0">
    <channel xmlns:g="http://base.google.com/ns/1.0">
        <title>BaSzErr - blog:2012:05:03</title>
        <description></description>
        <link>https://baszerr.eu/</link>
        <lastBuildDate>Thu, 30 Apr 2026 21:16:47 +0000</lastBuildDate>
        <generator>FeedCreator 1.8</generator>
        <image>
            <url>https://baszerr.eu/lib/exe/fetch.php?media=wiki:dokuwiki.svg</url>
            <title>BaSzErr</title>
            <link>https://baszerr.eu/</link>
        </image>
        <item>
            <title>1</title>
            <link>https://baszerr.eu/doku.php?id=blog:2012:05:03:1</link>
            <description>
&lt;h1 class=&quot;sectionedit1&quot; id=&quot;variadic_template_homework&quot;&gt;2012.05.03 - variadic template homework&lt;/h1&gt;
&lt;div class=&quot;level1&quot;&gt;

&lt;p&gt;
&lt;a href=&quot;https://baszerr.eu/lib/exe/detail.php?id=blog%3A2012%3A05%3A03%3A1&amp;amp;media=blog:2012:05:03:isoneof_andrei.png&quot; class=&quot;media&quot; title=&quot;blog:2012:05:03:isoneof_andrei.png&quot;&gt;&lt;img src=&quot;https://baszerr.eu/lib/exe/fetch.php?media=blog:2012:05:03:isoneof_andrei.png&quot; class=&quot;mediaright&quot; align=&quot;right&quot; loading=&quot;lazy&quot; title=&quot;template wheel - based on a true code :)&quot; alt=&quot;template wheel - based on a true code :)&quot; /&gt;&lt;/a&gt;
finally i got some spare time and finish watching recordings from &lt;a href=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012&quot; rel=&quot;ugc nofollow&quot;&gt;Going Native 2012&lt;/a&gt;. during one of the &lt;a href=&quot;https://en.wikipedia.org/wiki/Andrei Alexandrescu&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/Andrei Alexandrescu&quot;&gt;Andrei Alexandrescu&lt;/a&gt;&amp;#039;s &lt;a href=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Variadic-Templates-are-Funadic&quot; class=&quot;urlextern&quot; title=&quot;http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Variadic-Templates-are-Funadic&quot; rel=&quot;ugc nofollow&quot;&gt;presentations on variadic templates&lt;/a&gt; viewers were given a homework to be done: implementing &lt;a href=&quot;https://en.wikipedia.org/wiki/cartesian product&quot; class=&quot;interwiki iw_wp&quot; title=&quot;https://en.wikipedia.org/wiki/cartesian product&quot;&gt;cartesian product&lt;/a&gt; of the given template arguments, to exercise variadics a little.
&lt;/p&gt;

&lt;p&gt;
since the task is originally underspecified, let us stick to the number values, instead of (usual) types (for the sake of outputting) and assume the simple notion of generating result by directly printing each pair on the screen, instead of “saving” it as a type for later expansion (saves extra code to print-out the result in test/toy app). of course all of the computations are done in the compile time (except of printing itself ;)).
&lt;/p&gt;

&lt;p&gt;
so first let us define the interface, that will be used:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; expand&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  Cart&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;template&lt;/span&gt; go&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
it takes sequence of numbers as the argument and prints them out using &lt;em&gt;Cart&lt;/em&gt; template class and its &lt;em&gt;go()&lt;/em&gt; template method. they both take expanded arguments (i.e. values) lists. this enables us to iterate over the twice-expanded arguments.
&lt;/p&gt;

&lt;p&gt;
having this done, we implement the most generic class, that will be used to stop compile-time iteration:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Cart
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Us&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; go&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
now comes the fun part – iteration over the list. first we&amp;#039;ll define previous template&amp;#039;s specialization, that splits arguments into head (&lt;em&gt;H&lt;/em&gt;) and tail of “T” elements (&lt;em&gt;Ts&lt;/em&gt; – “s” is for plural):
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; H1&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Cart&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;H1&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
now &lt;em&gt;go()&lt;/em&gt; template (static) method is being implemented. what it does is to call helper sub-class (also variadic template), that will creates elements for all arguments, paired with the current one. class is called &lt;em&gt;X&lt;/em&gt;, in short. next step is to call instance of own class, but with different arguments – implementation moves to the next class-template argument:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; H1&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Cart&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;H1&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Us&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; go&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    X&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Us...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;go&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
    Cart&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;template&lt;/span&gt; go&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Us...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
note that last line – &lt;em&gt;Cart&amp;lt;&lt;strong&gt;Ts…&lt;/strong&gt;&amp;gt;::template go&amp;lt;&lt;strong&gt;Us…&lt;/strong&gt;&amp;gt;()&lt;/em&gt;. we move to the next argument with &lt;em&gt;Ts&lt;/em&gt; BUT keep the original, full list &lt;em&gt;Us&lt;/em&gt; for &lt;em&gt;go()&lt;/em&gt;, so that next argument can be paired up with the full list of elements again!
&lt;/p&gt;

&lt;p&gt;
now it&amp;#039;s time for &lt;em&gt;X&lt;/em&gt; to appear – this is simple metaprogram, that prints first argument of the class it is part of and then another arguments, given as its arguments. so the full &lt;em&gt;Cart&lt;/em&gt;&amp;#039;s class specialization looks like this:
&lt;/p&gt;
&lt;pre class=&quot;code c&quot;&gt;template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; H1&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Ts&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; Cart&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;H1&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
  template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Us&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; go&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    X&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Us...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;go&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
    Cart&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Ts...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;template&lt;/span&gt; go&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Us...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp;
  template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Us&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; X
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span class=&quot;kw4&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; go&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt; &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
  template&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt; H2&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;int&lt;/span&gt;...&lt;span class=&quot;me1&quot;&gt;Us&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;kw4&quot;&gt;struct&lt;/span&gt; X&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;H2&lt;span class=&quot;sy0&quot;&gt;,&lt;/span&gt; Us...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
    &lt;span class=&quot;kw4&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kw4&quot;&gt;void&lt;/span&gt; go&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;
    &lt;span class=&quot;br0&quot;&gt;&amp;#123;&lt;/span&gt;
      cout&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;H1&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;st0&quot;&gt;&amp;quot;:&amp;quot;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;H2&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;endl&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
      X&lt;span class=&quot;sy0&quot;&gt;&amp;lt;&lt;/span&gt;Us...&lt;span class=&quot;sy0&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span class=&quot;me2&quot;&gt;go&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#40;&lt;/span&gt;&lt;span class=&quot;br0&quot;&gt;&amp;#41;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;
  &lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;br0&quot;&gt;&amp;#125;&lt;/span&gt;&lt;span class=&quot;sy0&quot;&gt;;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;
example output for:
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;expand&amp;lt;1,2,66&amp;gt;();&lt;/pre&gt;

&lt;p&gt;
gives (i.e. prints):
&lt;/p&gt;
&lt;pre class=&quot;code&quot;&gt;1:1
1:2
1:66
2:1
2:2
2:66
66:1
66:2
66:66&lt;/pre&gt;

&lt;p&gt;
clean, short and simple! :) here is the full source of the &lt;a href=&quot;https://baszerr.eu/lib/exe/fetch.php?media=blog:2012:05:03:vt_cp.cpp&quot; class=&quot;media mediafile mf_cpp&quot; title=&quot;blog:2012:05:03:vt_cp.cpp (738 B)&quot;&gt;cartesian product&lt;/a&gt; implementation.
&lt;/p&gt;

&lt;/div&gt;
</description>
            <author>anonymous@undisclosed.example.com (Anonymous)</author>
            <pubDate>Tue, 15 Jun 2021 20:09:15 +0000</pubDate>
        </item>
    </channel>
</rss>
