Skarmander wrote:
> That said, "macros are evil" is primarily a cry from the C++ camp, see
> for example
> http://www.parashift.com/c++-faq-lit...s.html#faq-9.5
Hmmm, I don't think there is anything that only applies to C++ but several
reasons are given in that FAQ so it could be interesting reading even for
a C programmer. Multiple evaluation of side-effects, the absolute
disrespect for scoping rules, the fact that they can be replaced with
inline functions (in many cases) are those that immediately jump to my
mind.
> While preferring inline functions and constants to macros is good
> advice, C programmers can't and won't do it. Can't because, before C99
> (which will take another, oh, ten years or so to catch on), there was no
> standard mechanism for inlining functions, won't because it's a staple
> of C culture.
I hope I misunderstand you here, or are you really saying that macro
functions are part of C culture and therefore you are using them even if
there are better alternatives?
Anyhow, I never found a compiler that did not understand inline (yes, a few
of them needed __inline or __inline__, a case where the preprocessor can
be made good use of). I admit I don't do much programming on legacy
systems though, so it might be possible that such compilers still exist.
None of the compilers I did and do C with claimed C99 conformance btw,
except for newer gccs perhaps.
> The use of #define for symbolic constants is so ingrained that it will
> probably remain part of C until the time_t's wrap around, but luckily
> their use is disciplined enough not to cause much trouble... usually.
AFAIK, C has another big problem in that aspect, a constant does not
constitute an 'integral constant expression', i.e. it can't be used
specify the size of an array. I'm rather sure of this for C89, not so sure
about C99.
Hmm, maybe this is the reason you attribute 'macros are evil' to C++ since
in C++ 'inline' is part of the language and constants can be used to
declare arrays, so two possible reasons that you need them are gone.
> Ditto on include guards: #ifndef HEADER_H #define HEADER_H... Go beyond
> that and it quickly goes downhill.
Those are the best present solution for something that should be a proper
part of the language. It doesn't justify using macros in places where a
better alternative exists.
> The preprocessor unlocks power programmers are all too eager to (ab)use.
> More than a few times I've stared at a jungle of ALL-CAPS code where
> "convenient" macros effectively defined a completely new language
> because the perpetrators either loathed typing or apparently believed
> that by hiding crud behind macros, it stops being crud. When you have to
> tell your compiler to give you the preprocessor output to pore over
> because you're tired of chasing include files for the definitions, you
> know you're having a bad day.
My heart feels with you. I've been refactoring someone else's code for the
last three weeks and will probably go on with it this week. This code
didn't contain an overabundance of macros but showed a serious
misunderstanding of how to use a typed language and OO design. It's not
even C FWIW.
Uli