c++linuxgcc4.7

How to #define call(Double X) to func2(double X)


I have some 200 files where we have used "abs" function to find absolute for ints , longs and doubles . For doubles the right function call is fabs and not abs . Now in C++11 that is handled as abs supports for doubles as well in c++11 , but we are currently on old compiler .

The shift to C++11 is planned for next year and we need a fix intermittently .

Now all of these files have a header called as StdAfx.h in the beginning . So I want something like #define abs(X) fabs(X) . However the problem is that these files also have other headers like math.h etc where abs and fabs are actually declared which makes these code uncompilable .

Gcc says abs is being redeclared because those c++ standerad headers have declaration for abs and fbs.

So I want to use some statement in StdAfx.h such that only "CALL" to abs(double x) gets translated into fabs(double X ) but not its definition .

Kindly guide me .


Solution

  • You don't need a macro; just overload abs in the common header file:

    static double abs(double x) { return fabs(x); }