5

Trying to fix this issue: C++ How can I prevent my team developers from using integer version of abs by mistake? by using macro to make abs function not usable anymore.

If I compile a code containing myabs(3); with g++ option -Dmyabs=abs it compiles (myabs being replaced by abs), fine.

Now, if I compile a code containing abs(3); with g++ option -Dabs=forbidden it compiles too...why it does not report that forbidden is unknown? Looks like abs is not replaced by forbidden during pre-processing...why?

Community
  • 1
  • 1
jpo38
  • 20,821
  • 10
  • 70
  • 151

1 Answers1

1

Looks like abs is not replaced by forbidden during pre-processing...why?

At least the standard library headers that I use (libstdc++) which define ::abs, undefine your macro:

// Get rid of those macros defined in <math.h> in lieu of real functions.
#undef abs
#undef div
// ...

Your headers could be doing the same thing. Given such undefinition, it is indeed impossible to ban such function using a preprocessor macro.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • If it is guarded by include guards you could force its inclusion with the -include cmath compiler argument then define the abs macro (untried) – matt Jul 21 '16 at 10:14
  • Good answer: `abs` is replaced by `forbidden` before I include `math`, not after, so one must do `#undef abs` – jpo38 Jul 21 '16 at 11:46