Why does template parameter unpacking sometimes not work for std::function?
For convenience, let's call the three failed calls in your code #1, #2 and #3.
The problem is, when template arguments corresponding to a template parameter pack are explicitly specified, does the template parameter pack still participates in template argument deduction, and if it does, does deduction fail makes the whole call ill-formed?
From [temp.arg.explicit]/9:
Template argument deduction can extend the sequence of template arguments corresponding to a template parameter pack, even when the sequence contains explicitly specified template arguments.
We can infer that the template argument deduction should still be performed.
In the declaration of func_tmpl1
, std::function<A(Fs..., B)>
is a non-deduced context ([temp.deduct.type]/9: "If the template argument list of P contains a pack expansion that is not the last template argument, the entire template argument list is a non-deduced context."), so template argument deduction for Fs
should be ignored and #1 and #2 are both well-formed. There is a GCC bug report.
For #3, template argument deduction obviously fails (std::function<A(Fs...)>
vs a lambda type), but does deduction fail really make the code ill-formed? In my opinion, the standard is unclear about this, and there is a related issue. From the response of CWG, #3 is indeed ill-formed.