#include <iostream>
using namespace std;
int f(int x)
{
return x * x;
}
template <class X>
X ft(X x)
{
return x * x;
}
template <class F, class X>
X apply(F f, X x)
{
return f(x);
}
int main()
{
cout << apply(f, 2) << endl; // OK
cout << apply(ft, 2) << endl; // fail
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGYoaW50IHgpCnsKICByZXR1cm4geCAqIHg7Cn0KCnRlbXBsYXRlIDxjbGFzcyBYPgpYIGZ0KFggeCkKewogIHJldHVybiB4ICogeDsKfQoKdGVtcGxhdGUgPGNsYXNzIEYsIGNsYXNzIFg+ClggYXBwbHkoRiBmLCBYIHgpCnsKICByZXR1cm4gZih4KTsKfQoKCmludCBtYWluKCkKewogIGNvdXQgPDwgYXBwbHkoZiwgMikgPDwgZW5kbDsgLy8gT0sKICBjb3V0IDw8IGFwcGx5KGZ0LCAyKSA8PCBlbmRsOyAvLyBmYWlsCiAgcmV0dXJuIDA7Cn0K
prog.cpp: In function ‘int main()’:
prog.cpp:25:22: error: no matching function for call to ‘apply(<unresolved overloaded function type>, int)’
cout << apply(ft, 2) << endl; // fail
^
prog.cpp:16:3: note: candidate: template<class F, class X> X apply(F, X)
X apply(F f, X x)
^~~~~
prog.cpp:16:3: note: template argument deduction/substitution failed:
prog.cpp:25:22: note: couldn't deduce template parameter ‘F’
cout << apply(ft, 2) << endl; // fail
^