I can't use typedef with template, how can I change it? There is a syntax error "a typedef template is illegal". I want to use print()
function like this: print<double>(1, 2, add);
for any kind of data type.
#include <iostream>
using namespace std;
template <typename type> /*typedef*/ type(*calculator)(type, type);
template <typename type> inline const type& add(type x, type y) { return (type) x + y; }
template <typename type> inline const type& sub(type x, type y) { return (type) x - y; }
template <typename type> inline const type& mul(type x, type y) { return (type) x * y; }
template <typename type> inline const type& dev(type x, type y) { return (type) x / y; }
template <typename type> void print(type x, type y, calculator func) {
cout << "(" << x << "," << y << ")=" << func(x, y);
}
int main(void) {
print<float>(1, 2, add);
print<float>(1, 2, sub);
print<float>(1, 2, mul);
print<float>(1, 2, dev);
}