问题描述
我具有此功能:
template <typename... Args>
void f(Args... args, int last)
{
}
如果没有显式模板参数就调用模板推导将失败:
Template deduction fails if I call it without explicit template parameters:
f(2, 2); // candidate expects 1 argument, 2 provided
但是为参数包提供显式模板参数是可行的:
But giving the explicit template parameters for the parameter pack works:
f<int>(2, 2); // compiles fine
即使从逻辑上讲,编译器也应该能够推断出参数包由除了最后一个参数类型之外的所有参数。我该如何解决?
Even though logically speaking, the compiler should be able to deduce that the parameter pack consists of all but the last argument types. How would I fix this?
推荐答案
[temp.deduct.type] / p5:
[temp.deduct.type]/p5:
- [...]
- 在 parameter-declaration-list 末尾不会出现的功能参数包。
- [...]
- A function parameter pack that does not occur at the end of the parameter-declaration-list.
要扣除,您必须做
template <typename... Args>
void f(Args... args)
{
}
切掉正文中的最后一个参数,或者先做 last
first
:
and slice off the last argument in the body, or make last
first
instead:
template <typename... Args>
void f(int first, Args... args)
{
}
由于我们不知道此功能模板应该做什么,因此很难给出更具体的建议。
It's hard to give more specific advice since we don't know what this function template is supposed to do.
这篇关于参数打包后,模板推导失败,并带有参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!