Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't make a standard function forbidden using a preprocessor statement?

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?

like image 267
jpo38 Avatar asked Nov 09 '22 12:11

jpo38


1 Answers

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.

like image 143
eerorika Avatar answered Nov 15 '22 11:11

eerorika