Variadic function template with pack expansion not in last parameter

c++, c++11, function-templates, templates, variadic-templates

Solution

Because when a function parameter pack is not the last parameter, then the template parameter pack cannot be deduced from it and it will be ignored by template argument deduction.

So the two arguments `0, 0` are compared against `, int`, yielding a mismatch.

Deduction rules like this need to cover many special cases (like what happens when two parameter packs appear next to each other). Since parameter packs are a new feature in C++11, the authors of the respective proposal drafted the rules conservatively.

Note that a trailing template parameter pack will be empty if it is not otherwise deduced. So when you call the constructor with one argument, things will work (notice the difference of template parameter pack and function parameter pack here. The former is trailing, the latter is not).

Problem

I am wondering why the following code doesn't compile: ``` struct S { template <typename... T> S(T..., int); }; S c{0, 0}; ``` This code fails to compile with both clang and GCC 4.8. Here is the error with clang: ``` test.cpp:7:3: error: no matching constructor for initialization of 'S' S c{0, 0}; ^~~~~~~ test.cpp:4:5: note: candidate constructor not viable: requires 1 argument, but 2 were provided S(T..., int); ^ ``` It seems to me that this should work, and T should be deduced to be a pack of length 1. If the standards forbids doing things like this, does anyone know why?

Original source

Related problems