gpt4 book ai didi

c++ - 编译器无法推断出 std::max 的重载

转载 作者:可可西里 更新时间:2023-11-01 16:36:07 25 4
gpt4 key购买 nike

用我的编译器

typedef const double&(*fT)(const double&, const double&);
typedef std::function<const double&(const double&, const double&)> std_func;

fT f1 = std::max<double>; //(1)
std_func f2 = static_cast<fT>(std::max<double>); //(2)
std_func f3 = f1; //(3)

(1, 2, 3) 工作但是

auto f4 = std::max<double>;                          //(4)
std_func f5 = std::max<double>; //(5)

(4, 5) 不要。编译器提示它无法为案例 5 选择重载。

这种行为正常吗?

什么是最便携最正确的写法?

最佳答案

std::max<double> 的实例化有两种可能的重载: std::max(double, double)std::max(std::initializer_list<double>) .因此,版本 4 和 5 失败,因为它无法确定哪个重载匹配。

情况 1、2 和 3 由于特殊规则而成功 - 当获取重载函数的地址时,结果的类型用于选择适当的重载。

关于c++ - 编译器无法推断出 std::max 的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50586838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com