且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当将重载函数作为参数时,模板参数推导如何工作?

更新时间:2023-11-10 20:05:28

如所链接问题的答案中所述:

As noted in the answer to your linked question:

[temp.deduct.call]/6:P是函数类型时,指向函数类型的指针或指向成员函数类型的指针:

[temp.deduct.call]/6:When P is a function type, pointer to function type, or pointer to member function type:

-如果参数是包含一个或多个函数模板的重载集,则将参数视为 作为非推论上下文.

— If the argument is an overload set containing one or more function templates, the parameter is treated as a non-deduced context.

由于重载集包含一个功能模板,因此该参数被视为非推导上下文.这会导致模板参数推导失败:

Since the overload set contains a function template, the parameter is treated as a non-deduced context. This causes template argument deduction to fail:

[temp.deduct.type]/4: [...]如果仅在非推导中使用模板参数 上下文且未明确指定,模板参数推导失败.

[temp.deduct.type]/4: [...]If a template parameter is used only in non-deduced contexts and is not explicitly specified, template argument deduction fails.

这个失败的推论给你你的错误.请注意,如果您明确指定参数,则代码将成功编译:

And this failed deduction gives you your error. Note that if you explicitly specify the arguments, the code compiles successfully:

bar<int,double>(foo);

实时演示