Can't disable a standard function using preprocessor instruction?

Trying to fix this problem: C ++ How can I prevent the developers of my team from using the integer version of abs by mistake? Using a macro to make abs not more useful.

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

Now, if I compile code containing abs(3); with the g ++ -Dabs=forbidden option, it compiles too ... why doesn't it report that forbidden unknown? It looks like abs not being replaced with forbidden during preprocessing ... why?

+5
source share
1 answer

It looks like abs is not being replaced with prohibited during preprocessing ... why?

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

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

Your headlines can do the same. Given such an indefinite definition, it is really impossible to prohibit such a function using a preprocessor macro.

+1
source

All Articles