变量函数(没有参数!)

Han*_*ans 5 templates c++11

假设您想在C++ 0x中执行此操作:

size_t count_int() { return 0; }
template<typename T, typename... Tn>
size_t count_int(T a0, Tn... an) {
    size_t n = is_integer<T>::value ? 1 : 0;
    return n + count_int(an...);
}
Run Code Online (Sandbox Code Playgroud)

很好,但感觉没有必要传递参数.不幸的是,这不起作用:

size_t count_int() { return 0; }
template<typename T, typename... Tn>
size_t count_int() {
    size_t n = is_integer<T>::value ? 1 : 0;
    return n + count_int<Tn...>();
}
Run Code Online (Sandbox Code Playgroud)

GCC抱怨错误:在倒数第二行中调用'count_int()'没有匹配函数.为什么以及如何解决这个问题?谢谢.

Pup*_*ppy 1

它会失败,因为当参数包不包含任何值时,您没有基本情况 - 因为您之前使用的 count_int() 基本情况没有模板化,因此无法使用 调用<Tn...>,即使Tn为空也是如此。这就是它失败的原因。至于如何修复它,我几乎不知道。