以下代码片段是优化算法的精简版本:
template<typename F>
double helper(F f, double x)
{
return x;
}
template<typename F, typename L>
double optimize(F f, double x, L line_search)
{
double y = line_search(f, x);
return y;
}
int main()
{
auto f = [](double x){ return (x - 2) * (x - 2); };
double solution = optimize(f, -2, helper);
}
我使用模板参数 optimize
参数化了主函数 ( helper
) 和辅助函数 ( F
)对于他们试图最小化的功能。但是,代码无法编译;编译器无法确定 line_search
的类型:
t04.cpp: In function ‘int main()’:
t04.cpp:17:45: error: no matching function for call to ‘optimize(main()::<lambda(double)>&, int, <unresolved overloaded function type>)’
double solution = optimize(f, -2, helper);
^
t04.cpp:8:8: note: candidate: template<class F, class L> double optimize(F, double, L)
double optimize(F f, double x, L line_search)
^~~~~~~~
t04.cpp:8:8: note: template argument deduction/substitution failed:
t04.cpp:17:45: note: couldn't deduce template parameter ‘L’
double solution = optimize(f, -2, helper);
(我使用的是 gcc v7.5.0,但我认为这在这里并不重要。)我想我明白为什么编译器不知道 line_search
的类型(它不会在 optimize
内部查看 line_search
是如何被调用的),但我不知道如何解释它正确的类型应该是什么。我希望我能说optimize(f, -2, helper<type of f>)
,但我没有找到如何做到这一点。我需要对代码进行哪些更改?
顺便说一句,如果有其他同样通用的设计方法,但可以避免模板参数推导问题,我将有兴趣了解它们。
最佳答案
您可以为 L
添加默认类型,其中包括 F
:
template<typename F>
double helper(F f, double x) {
return f(x);
}
template<typename F, typename L = double(*)(F, double)> // <- here
double optimize(F f, double x, L line_search) {
double y = line_search(f, x);
return y;
}
当您这样做时,它会找到正确的模板实例化
double solution = optimize(f, -2, helper); // helper is helper<decltype(f)>
如果helper
是您经常使用的函数模板,您可以将其设为默认:
template<typename F, typename L = double(*)(F, double)>
double optimize(F f, double x, L line_search = helper<F>) { // <- here
double y = line_search(f, x);
return y;
}
这使得可以在不指定帮助器的情况下调用optimize
:
double solution = optimize(f, -2); // helper<decltype(f)> is the default
关于c++ - 无法推断间接调用模板函数的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68169702/