[Mono-dev] x86 code generator

Cody Russell bratsche at gnome.org
Sun Jul 2 22:57:03 EDT 2006


On Sun, 2006-07-02 at 17:53 -0700, Rusmin Susanto wrote:
> 3. Why do you write everything in
>  
>    do .. while(0)   pattern?
>  
>    while(0) will always fail (I think). So there is no loop

So macros behave more like how you expect them to.  Try this:

#define FOO(x) { return x; }

int main()
{
    int a;
    if (a)
       FOO(a);
}

That works fine.  But change it to this:

int main()
{
    if (a)
        FOO(a);
    else
        return a;
}

...and the compiler will hate you.  The reason begin that you're trying
to use FOO like you would a function statement (ie, the semicolon).  If
you change the call to just "FOO(x)" without the semicolon, it works
fine and the compiler doesn't flip out when it finds the else statement.

#define FOO(x) do { return x; } while(0)

This makes the macro behave more like a function, so things like if/else
will work the way you expect.







More information about the Mono-devel-list mailing list