Using fabs for double and integers.


I was trying to create a generic class and I wanted to know if the fabs() can be applied on both floating point and integer values? Please advise.

In short: it can. When you pass integers to fabs() they will be promoted to double.
C doesn't have overloading so they created different functions to work with all types, abs, labs, fabs, fabsf, etc.

C++ has added overloads to abs and fabs to make it work with all integer and floating point types. Before C++11 there was no integer overloads of fabs so if you tried to pass an integer to it it would give you an error.
Last edited on
it would give you an error.

It won't

I just tried it on not supporting C++11 compiler. Works fine.
The problem is that if you have an overloaded function and you pass a value that doesn't match any of the functions exactly but could implicitly be converted to match more than one of the functions. In that case it doesn't know which function to choose so it gives an error. Some compilers added extra fabs overloads earlier but it was not standard until C++11.
Topic archived. No new replies allowed.