> Or the belief that languages shouldn't have preprocessors because the
> C preprocessor was so gross; inevitably you lose major functionality
> like "#ifdef 0/#endif" and the language designers defend it to the
> end.
I'm glad to see people acknowledging that the C preprocessor is a powerful =
tool and not something to be shunned unconditionally. Used improperly, you =
can cut off your own leg, but the same is true for a chainsaw - yet both =
are still useful tools.
I'm so sick of hearing over and over again about the evils of preprocessors=
that I'm writing an article for Game Programming Gems 3 on "Finding =
Redeeming Value in C-Style Macros." Since the article is still in-progress,=
I was wondering if I could pick the brains of the people on this list... =
Currently, I have about eight pretty good macro tricks, but I'm open to =
adding more.
Just to throw one out there, I'll offer the compile time assert:
#define cassert(expn) typedef char __C_ASSERT__[(expn)?1:-1]
For example, if you're working on cross-platform code, you might want to =
check at compile time that enumerations are the same size as an unsigned =
int. Using the previous macro, you can check this by writing:
cassert( sizeof(MyEnum) =3D=3D sizeof(unsigned int) );
As you can see, a good macro trick is something that creatively exploits =
text replacement and makes things easier to understand - not harder. :)
-Steve
|