如何使用参数包调用函数的重载版本?这大致就是我想做的:
void foo(int x=5) {
// Call foo<Args...>(x) here
}
template <typename... Args>
void foo(int x, Args&&... args) {
}
有可能吗?还是我需要不同的函数名称?
您可以通过显式指定模板参数来调用模板版本。如果没有要指定的模板参数,则可以指定空列表。例如
template <typename... Args>
void foo(int x, Args&&... args) {
}
void foo(int x=5) {
foo<>(x);
}
由于模板化函数将int
作为非默认参数,因此非模板化函数完全不接受int
毫无意义。传递int
值将同时满足这两个功能,从而导致模棱两可。因此,我建议完全删除非模板函数的int
参数:
template <typename... Args>
void foo(int x, Args&&... args) {
...
}
void foo() {
foo<>(5);
}