0

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 .

MAG
  • 2,841
  • 6
  • 27
  • 47
  • What you want is not possible. Rewrite your code instead. – Konrad Rudolph Jun 27 '16 at 15:37
  • I'm not really sure why you think this would be preferable to just fixing the affected bits of code. It might get you able to do a quick build, but it certainly does not seem to be worth it from a long-term maintenance perspective. – Cody Gray - on strike Jun 27 '16 at 15:40

1 Answers1

3

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

static double abs(double x) { return fabs(x); }
Boann
  • 48,794
  • 16
  • 117
  • 146
  • Also consider to define the overload in a operate namespace (e.g., my_abs_double_overload) and then bring that namespace into scope using a "using namespace my_abs_double_overload" statement (intention more clear and using can be #ifdef'd on C++11 support)? – Kjell-Olov Högdahl Jun 27 '16 at 16:29