Note that there are some explanatory texts on larger screens.

plurals
  1. POConditional compile-time inclusion/exclusion of code based on template argument(s)?
    primarykey
    data
    text
    <p>Consider the following class, with the inner struct <code>Y</code> being used as a type, eg. in templates, later on:</p> <pre><code>template&lt;int I&gt; class X{ template&lt;class T1&gt; struct Y{}; template&lt;class T1, class T2&gt; struct Y{}; }; </code></pre> <p>Now, this example will obviously not compile, with the error that the second <code>X&lt;I&gt;::Y</code> has already been defined or that it has too many template parameters.<br> I'd like to resolve that without (extra) partial specialization, since the <code>int I</code> parameter isn't the only one and the position of it can differ in different partial specializations (my actual struct looks <a href="http://ideone.com/9R2IS" rel="noreferrer">more like this</a>, the above is just for simplicity of the question), so I'd like <code>one class fits every I</code> solution.</p> <hr> <p>My first thought was obviously <code>enable_if</code>, but that seems to fail on me, eg. I still get the same errors:</p> <pre><code>// assuming C++11 support, else use boost #include &lt;type_traits&gt; template&lt;int I&gt; class X{ template&lt;class T1, class = std::enable_if&lt;I==1&gt;::type&gt; struct Y{}; template&lt;class T1, class T2, class = std::enable_if&lt;I==2&gt;::type&gt; struct Y{}; }; </code></pre> <hr> <p>So, since <code>enable_if</code> fails, I hope there is another way to achieve the following compile time check:</p> <pre><code>template&lt;int I&gt; class X{ __include_if(I == 1){ template&lt;class T1&gt; struct Y{}; } __include_if(I == 2){ template&lt;class T1, class T2&gt; struct Y{}; } }; </code></pre> <hr> <p>It would just be to save me <strong>a lot</strong> of code duplication, but I'd be really happy if it was somehow possible.<br> Edit: Sadly, I can't use the obvious: variadic templates, as I'm using Visual Studio 2010, so only the C++0x stuff that is supported there I can use. :/</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.
 

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