我对一个简单的可变参数模板函数感到沮丧:
constexpr size_t num_args () {
return 0;
}
template <typename H, typename... T>
constexpr size_t num_args () {
return 1 + num_args <T...> ();
}
int main () {
std :: cout << num_args <int, int, int> ();
}
上面没有编译,详见上面链接的问题和后续,但是下面的函数可以编译
template <typename T, typename... Args> void foo (T, Args...);
template <typename... Args> void foo (int, Args...);
void foo () {}
template <typename... Args>
void foo (int x, Args... args)
{
std :: cout << "int:" << x;
foo (args...);
}
template <typename T, typename... Args>
void foo (T x, Args... args)
{
std :: cout << " T:" << x;
foo (args...);
}
foo (int (123), float (123)); // Prints "int:123 T:123.0"
两者似乎都使用相同的语言机制,但是为什么第一个不好而第二个很好呢?