Brian Paul wrote:
> 
> I've seen this before but I can't recall the reasoning for it.
> When defining a multi-statement macro people use this construct:
> 
> #define MY_MACRO(FOO)      \
>    do {                    \
>         statement1(FOO);   \
>         statement2(FOO);   \
>         statement3(FOO);   \
>    } while(0)
> 
> Why is the do/while loop used?
> 
> I'm asking here so everyone can be educated.

To force the compound statements to behave as a single statement, eg in if/else
statements:

        if (1)
                MY_MACRO(_foo);

works as expected with the do/while, but gives some funny results without.  More
complicated examples can be constructed.

It also _forces_ the coder to place a semicolon after the statement, as in:

        MY_MACRO(1);
        MY_MACRO(2);

which isn't anything to do with correctness, but it does mean that emacs
auto-indent will continue to work correctly for these lines. 

Keith


_______________________________________________
Mesa-dev maillist  -  [EMAIL PROTECTED]
http://lists.mesa3d.org/mailman/listinfo/mesa-dev

Reply via email to