What is the worst real-world macros/pre-processor abuse you've ever come across (please no contrived IOCCC answers *haha*)?
Please add a short snippet or story if it is really entertaining. The goal is to teach something instead of always telling people "never use macros".
p.s.: I've used macros before... but usually I get rid of them eventually when I have a "real" solution (even if the real solution is inlined so it becomes similar to a macro).
Bonus: Give an example where the macro was really was better than a not-macro solution.
Related question: When are C++ macros beneficial?
I was bored one day and was playing around with blocks in Objective-C...
allowing "interesting" things like:
(some function and class definitions not shown for sake of brevity)
The worst I've ever encountered was in a product containing a suite of executables where the designated technical leader hadn't figured out libraries.
Instead, he had sets of files that were shared in several Visual Source Safe folders. He then realised they needed to behave slightly differently for each application.
There's a number of refactoring steps you could apply here.
Instead, he used #ifdefs
By a classmate who failed to understand the rules about magic numbers:
#define TWO_HUNDRED_AND_EIGHTY_THREE_POINT_ONE 283.1
The worst one I saw was the non-use :-)
Someone wrote a strcpy (I think that was it... over 10 years ago now) function inside of a method (because they didn't want the overhead of calling strcpy... sigh).
They clued in that it wouldn't work for Japanese characters so they added an "if" at the start to do ASCII or Unicode. At that point the code was about a screen long... likely killing cache coherency and erasing his supposed savings for the inlining of the code.
The code was identical save for the types (so should have used a macro).
Of course the strcpy that they wrote was much much much slower than the hand tuned assembler one that was in the standard library...
Of course if they had just done it all as a macro it could have been replaced with a call to strcpy...
Of course I quit the company (not directly because of that...)
In one year of the International Obfuscated C Coding Contest, there was an entry where the entire program was:
P
With the proviso that you could define
P
in the makefile to be whatever program you wanted.As I recall, it won in one of the categories, and the next year a rule had popped up disallowing that style of entry.
(Edit: six months later or something... I'm sure the "No IOCCC" thing wasn't in the main question when I wrote this...)