I am experimenting with std::max. I am tring to pass integers constexpr with uniform initialization (curly braces), to compare them to floating-point variables.
Experiment a): Call std::max() with double/int mixed
double a = 3.0;
int b = 5;
auto res = std::max(a, b);
Does not compile. Clang reports error: no matching function for call to 'max'. This is of course OK.
Experiment b): Use curly braces + constexpr int for non-anrrowing conversion
double a = 3.0;
constexpr int b = 5;
auto res = std::max(a, {b});
Compiles and works as expected: returns a double with value 5.0.
Experiment c): Same as b) but swap arguments of std::max.
double a = 3.0;
constexpr int b = 5;
auto res = std::max({b}, a);
Does not compile both under gcc and clang. Why?
Clang reports error: called object type 'double' is not a function or function pointer.
std::max<double>(a, b)should work.max(a, b)doesn't compile because the compiler cannot deduce the template parameter - one argument says it'sint, the other says it'sdouble. Putting{b}in curly braces makes it non-deduced context, removing the ambiguity; the template parameter is now successfully deduced fromaalone. - Igor Tandetnik<algorithm>which is required to makestd::maxavailable. - walnut