Tobias Hermann Tobias Hermann - 1 year ago 85
C++ Question

type deduction failing for auto stdMaxInt = std::max<int>;

Using GCC 4.8.4 with

g++ --std=c++11 main.cpp
outputs the following error

error: unable to deduce ‘auto’ from ‘max<int>’
auto stdMaxInt = std::max<int>;

for this code

#include <algorithm>

template<class T>
const T& myMax(const T& a, const T& b)
return (a < b) ? b : a;

int main()
auto myMaxInt = myMax<int>;
myMaxInt(1, 2);

auto stdMaxInt = std::max<int>;
stdMaxInt(1, 2);

Why does it work with
but not with
? And can we make it work with

Answer Source

It's because std::max is an overloaded function, so it doesn't know which overload you want to create a pointer to. You can use static_cast to select the overload you want.

auto stdMaxInt = static_cast<const int&(*)(const int&, const int&)>(std::max<int>);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download