编译器无法推断 std::max 的重载
compiler cannot deduce overload of 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 成功是因为特殊规则 - 当采用过载函数的地址时,结果的类型用于选择适当的重载。
相关文章:
- 继承函数的重载解析
- 你能重载对象变量名本身返回的内容吗
- 从父命名空间重载类型
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- 重载操作程序时出错>>用于类中的字符串 memebr
- 一个关于在C++中重载布尔运算符的问题
- 不同翻译单元中不可重载的非内联函数定义
- 为什么使用SFINAE而不是函数重载
- 为什么我不能在 C++ 中的特定函数重载中调用同一函数的任何其他重载?
- 将重载的成员函数传递给函数模板
- c++:可变模板和函数重载
- 重载元组索引运算符-C++
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 重载==不适用于二进制树
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 编译器无法推断 std::max 的重载
- 当我尝试编译程序时,我遇到了一堆错误,例如:'std::max':找不到匹配的重载函数
- 错误:重载的“max(int, int)”的调用不明确
- 如何在不重载比较操作符的情况下为std::max专门化自定义类型