Note that there are some explanatory texts on larger screens.

plurals
  1. POPartial Specialization of tuple contents with variadic arguments
    text
    copied!<p>Currently, I'm trying to get some code to react differently to different types. This isn't the exact code, but it gets the message across.</p> <pre><code>template&lt;class A, class B&gt; struct alpha { enum { value = 0 }; }; template&lt;class T, class... Args&gt; struct alpha&lt;std::tuple&lt;Args...&gt;, T&gt; { enum { value = 1 }; }; // This gets ignored template&lt;class T, class... Args&gt; struct alpha&lt;std::tuple&lt;Args..., std::vector&lt;T&gt; &gt;, T&gt; { enum { value = 2 }; }; // This gets ignored template&lt;class T, class... Args&gt; struct alpha&lt;std::tuple&lt;Args..., T&gt;, T&gt; { enum { value = 3 }; }; template&lt;class T, class... Args&gt; struct alpha&lt;T, std::tuple&lt;Args...&gt; &gt; { enum { value = 4 }; }; template&lt;class... LArgs, class... RArgs&gt; struct alpha&lt;std::tuple&lt;LArgs...&gt;, std::tuple&lt;RArgs...&gt; &gt; { enum { value = 5 }; }; int main(int argc, char* argv[]) { std::cout &lt;&lt; alpha&lt;std::tuple&lt;int, double&gt;, double&gt;::value &lt;&lt; std::endl; // prints 1 return 0; } </code></pre> <p>I've tried more than this code shows, but nothing works so far and I ran across a problem with explicit specialization in a non-namespace scope. For reference, I'm working on gcc 4.6 (the one that comes with oneiric server), which I believe has complete variadic template support. I don't care how ugly it gets if the implementation works to detect the last argument of the parameter pack and the other types as well. Any suggestions?</p> <p>EDIT: I wanted to share the solution I used based on the answers (this is an example).</p> <pre><code>template&lt;typename T&gt; struct tuple_last; template&lt;typename T, typename U, typename... Args&gt; struct tuple_last&lt;std::tuple&lt;T,U,Args...&gt;&gt; { typedef typename tuple_last&lt;std::tuple&lt;U,Args...&gt;&gt;::type type; }; template&lt;typename T&gt; struct tuple_last&lt;std::tuple&lt;T&gt;&gt; { typedef T type; }; namespace details { // default case: template&lt;class T, class U&gt; struct alpha_impl { enum { value = 1 }; }; template&lt;class T&gt; struct alpha_impl&lt;T, T&gt; { enum { value = 101 }; }; template&lt;class T&gt; struct alpha_impl&lt;T, std::vector&lt;T&gt;&gt; { enum { value = 102 }; }; // and so on. } template&lt;class T, class... Args&gt; struct alpha&lt;std::tuple&lt;Args...&gt;, T&gt; : details::alpha_impl&lt;T, tuple_last&lt;std::tuple&lt;Args...&gt;&gt;; </code></pre>
 

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