Sensei <> writes:
> On 2005-10-10 15:31:40 +0200, Chris Dollin <> said:
>> Sensei wrote:
>>> So why I see so often something like:
>>> #define CEXCERPT do { \
>>> some(); \
>>> C_code(); \
>>> here(); \
>>> } while(0)
>>> Is it just some person thinking he would achieve more speed?
>> No, it's some person arranging that their macro can be expanded
>> as CEXCERPT;
>> without confusion. (I would have thought, even more likely
>> with a parameterised macro.)
>
> I understood why he does not use a ; at the end, but why someone would
> have such a big macro... is beyond my knowledge, something like the
> xge_whatever stuff I found...
One reason to do this is to save the overhead of a function call.
That's rarely worth the effort, though it can be if it's likely to be
invoked repeatedly in a loop. For example, putc() in <stdio.h> is
commonly implemented as a complex macro; likewise for the is*() and
to*() functions/macros in <ctype.h>.
Another reason is to do something that you can't do with a function,
even an inline one. For example, a function argument can only be an
expression; a macro argument can be a type name. The offsetof() macro
is an example that can't be implemented as a function.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.