【问题标题】:compiler cannot deduce overload of std::max编译器无法推断 std::max 的重载
【发布时间】:2018-05-29 14:25:36
【问题描述】:

用我的编译器

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 选择重载。

这种行为正常吗?

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

【问题讨论】:

  • 你想达到什么目的?
  • @Slava 我正在尝试在另一个编译器上修复旧代码库的编译错误。旧代码是 (5)。我通过写(3)来修复它。谢谢你的评论,欢迎回答:-)
  • 我认为您应该将您的问题重新表述为如何进行 (5) 编译以及您尝试了哪些变体,因为它是写的根本不清楚

标签: c++ stl std-function overload-resolution


【解决方案1】:

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

案例 1、2 和 3 之所以成功,是因为特殊规则——在获取重载函数的地址时,使用结果的类型来选择合适的重载。

【讨论】:

  • 感谢 SergeyA 的回答。因此,据我了解,我可以对解决方案 3 感到满意。
  • @jimifiki 所有这些都是可行的,具体取决于您的偏好。也有 3 个作品。
猜你喜欢
  • 1970-01-01
  • 2018-08-22
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 1970-01-01
相关资源
最近更新 更多