I wish to write a C++ template function which in turn uses some "C" functions and exploits function overloading.
For example, I need to write a function myAbs
using templates which make appropriate calls to fabs
or abs
defined in math.h
, based on the input parameter type. How to do this?
#include <math.h>
template<typename T>
T abs(T x)
{
// I need to write an efficient code here!
// If it is 'double' and 'float' I may be able to compare the
// sizeof(Type) and call 'return fabs(x)' or 'return abs(x)'.
// But this is not a good solution as two types can be of same size!
}
Note: I simply used it as an example to explain my question. I already know that such a function "abs" is already available in <cmath>
.