Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here you go:</p> <p><a href="http://ideone.com/AdEfl" rel="nofollow">http://ideone.com/AdEfl</a></p> <p>And the code:</p> <pre><code>#include &lt;iostream&gt; template &lt;int I&gt; struct Traits { struct inner{}; }; template &lt;&gt; struct Traits&lt;1&gt; { struct inner{ template&lt;class T1&gt; struct impl{ impl() { std::cout &lt;&lt; "impl&lt;T1&gt;" &lt;&lt; std::endl; } }; }; }; template &lt;&gt; struct Traits&lt;2&gt; { struct inner{ template&lt;class T1, class T2&gt; struct impl{ impl() { std::cout &lt;&lt; "impl&lt;T1, T2&gt;" &lt;&lt; std::endl; } }; }; }; template&lt;class T&gt; struct Test{}; template&lt;class T, class K&gt; struct Foo{}; template&lt;int I&gt; struct arg{}; template&lt; template&lt;class, class&gt; class T, class P1, int I &gt; struct Test&lt; T&lt;P1, arg&lt;I&gt; &gt; &gt;{ typedef typename Traits&lt;I&gt;::inner inner; }; template&lt; template&lt;class, class&gt; class T, class P2, int I &gt; struct Test&lt; T&lt;arg&lt;I&gt;, P2 &gt; &gt;{ typedef typename Traits&lt;I&gt;::inner inner; }; // and a bunch of other partial specializations int main(){ typename Test&lt;Foo&lt;int, arg&lt;1&gt; &gt; &gt;::inner::impl&lt;int&gt; b; typename Test&lt;Foo&lt;int, arg&lt;2&gt; &gt; &gt;::inner::impl&lt;int, double&gt; c; } </code></pre> <p>Explanation: Basically it's an extension of the idea of partial specialization, however the difference is that rather than specializing within <code>Test</code>, delegate to a specific class that can be specialized on <code>I</code> alone. That way you only need to define versions of <code>inner</code> for each <code>I</code> <em>once</em>. Then multiple specializations of <code>Test</code> can re-use. The <code>inner</code> holder is used to make the <code>typedef</code> in the <code>Test</code> class easier to handle. </p> <p>EDIT: here is a test case that shows what happens if you pass in the wrong number of template arguments: <a href="http://ideone.com/QzgNP" rel="nofollow">http://ideone.com/QzgNP</a></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.
 

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