First, I want to say that I know that the following is not possible in C++17, I am more interested in whether it could be possible.
Function templates and class templates differ in the aspect that the first declaration is possible while the second is not:
template<class ...Ts, class S>
void foo(S s);
template<class ...Ts, class S>
class bar;
In the accepted answer to this question it is pointed out that this is because the compiler can deduce S
from the function parameter, while it cannot do that for the class. So for the first code, it can differentiate between the Ts
and S
and it cannot in for the second code.
While I am still a bit unsure, why the compiler cannot deduce S
by virtue of using the last template parameter, I see why the function case is clearly superior.
But the following does compile
template<class ...Ts, class S>
void foo();
only one cannot call it in any way, since the compiler won't be able to deduce S
. So theoretically, could one allow the declaration of bar
to compile and outsource the error to whenever we want to use it? Then if we had no default constructor and a constructor like
bar::bar(S s, Ts... ts); // Edit: Added Ts which are necessary as @Nicol Bolas. And I am aware that it is weird to turn them around here but not in the template.
we could deduce S
from this, whenever we initialize an object of type bar
(except for copying/moving, but then one just takes the same type as the object passed in).
So why is this not possible? Is it because bar
is a type and one should be able to use it without instantiating an object of that type?