问题
This piece of code works fine with g++ and Clang:
template <typename Sig, Sig& S> struct OpF;
template <typename TR, typename ... Ts, TR (&f)(Ts...)>
struct OpF<TR (Ts...), f> {
};
int foo(int x) {
return 0;
}
OpF<int (int), foo> f;
But the new shiny VS2013 compiler bails out with
f.cpp(4) : error C3520: 'Ts' : parameter pack must be expanded in this context
Which one is at fault?
回答1:
This was a bug in the VS2013 compiler, which seems to have been fixed now. For a workaround, see: Visual C++ 12 (VS2013 Preview) variadic template with function parameter workaround. However, your code probably still won't compile, due to another bug in the VS2013 compiler, which causes it to parse the template argument TR (&f)(Ts...)
as TR (&f)(void)
. Though, there is a workaround for that too. You can use a nested class, which then can use the parameter pack. For example:
template <typename Sig> struct OpF;
template <typename TR, typename ... Ts>
struct OpF<TR (Ts...)> {
template <TR (&f)(Ts...)>
struct Fn {
};
};
int foo(int x) {
return 0;
}
OpF<int (int)>::Fn<foo> f;
Template parameters after a parameter pack are not allowed in template declarations. However, Clang and GCC do allow it in a template specialization. So it could be that Clang and GCC are being too liberal with the C++11 specs.
回答2:
That's not a bug (in VC++) there is a spare template parameter after the variadic list. This is not valid C++ 11.
Looks like a bug in GCC and CLang.
来源:https://stackoverflow.com/questions/17332749/vs2013-fails-with-variadic-template-specialization