Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compiler cannot deduce overload of std::max

With my compiler

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) work but

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

(4, 5) don't. The compiler complains about its incapability to choose the overload for case 5.

Is this behavior normal?

What is the most portable and correct way to write it?

like image 223
jimifiki Avatar asked May 29 '18 14:05

jimifiki


1 Answers

There are two possible overloads of instantiation of std::max<double>: std::max(double, double) and std::max(std::initializer_list<double>). Because of that, versions 4 and 5 fail, since it can't figure out which overload matches.

Cases 1, 2 and 3 succeed because of the special rules - when taking an address of overload function, type of the result is used to select the proper overload.

like image 117
SergeyA Avatar answered Nov 03 '22 12:11

SergeyA