Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I have multiple parameter packs in a variadic template?
    primarykey
    data
    text
    <p>Function <em>one()</em> accepts one parameter pack. Function <em>two()</em> accepts two. Each pack is constrained to be wrapped in types <em>A</em> and <em>B</em>. Why is it impossible to instantiate <em>two()</em>?</p> <pre><code>template &lt;typename T&gt; struct A {}; template &lt;typename T&gt; struct B {}; template &lt;typename... Ts&gt; void one(A&lt;Ts&gt; ...as) { } template &lt;typename... Ts, typename... Us&gt; void two(A&lt;Ts&gt; ...as, B&lt;Us&gt; ...bs) { } int main() { auto a = A&lt;int&gt;(); auto b = B&lt;int&gt;(); // Just fine one(); one(a); one(a, a); // All errors two(); two(a); two(a, b); } </code></pre> <p>Tried with gcc and clang.</p> <pre><code>sam@wish:~/x/cpp$ gcc -std=c++0x variadic_templates.cpp variadic_templates.cpp: In function ‘int main()’: variadic_templates.cpp:23:7: error: no matching function for call to ‘two()’ variadic_templates.cpp:23:7: note: candidate is: variadic_templates.cpp:11:6: note: template&lt;class ... Ts, class ... Us&gt; void two(A&lt;Ts&gt;..., B&lt;Us&gt;...) variadic_templates.cpp:24:8: error: no matching function for call to ‘two(A&lt;int&gt;&amp;)’ variadic_templates.cpp:24:8: note: candidate is: variadic_templates.cpp:11:6: note: template&lt;class ... Ts, class ... Us&gt; void two(A&lt;Ts&gt;..., B&lt;Us&gt;...) variadic_templates.cpp:25:11: error: no matching function for call to ‘two(A&lt;int&gt;&amp;, B&lt;int&gt;&amp;)’ variadic_templates.cpp:25:11: note: candidate is: variadic_templates.cpp:11:6: note: template&lt;class ... Ts, class ... Us&gt; void two(A&lt;Ts&gt;..., B&lt;Us&gt;...) sam@wish:~/x/cpp$ clang -std=c++0x variadic_templates.cpp variadic_templates.cpp:23:3: error: no matching function for call to 'two' two(); ^~~ variadic_templates.cpp:11:6: note: candidate function template not viable: requires at least 1 argument, but 0 were provided void two(A&lt;Ts&gt; ...as, B&lt;Us&gt; ...bs) {} ^ variadic_templates.cpp:24:3: error: no matching function for call to 'two' two(a); ^~~ variadic_templates.cpp:11:6: note: candidate function not viable: requires 0 arguments, but 1 was provided void two(A&lt;Ts&gt; ...as, B&lt;Us&gt; ...bs) {} ^ variadic_templates.cpp:25:3: error: no matching function for call to 'two' two(a, b); ^~~ variadic_templates.cpp:11:6: note: candidate function not viable: requires 0 arguments, but 2 were provided void two(A&lt;Ts&gt; ...as, B&lt;Us&gt; ...bs) {} ^ 3 errors generated. </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
 

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