编译器无法推断 std::max 的重载



使用我的编译器

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 成功是因为特殊规则 - 当采用过载函数的地址时,结果的类型用于选择适当的重载。

最新更新