Re: 'do {} while (0)' replacement

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Frank Mehnert wrote:
I wouldn't say that. Actually this statement is part of a bigger
project and I'm searching a universal macro which fits these needs.
for (;;)
{
 int bar = 1, buzz = 2;
 if (bar)
   BREAK_ON_ERROR(buzz);


 if (bar)
   BREAK_ON_ERROR(buzz);
 else
   BREAK_ON_ERROR(buzz);
}

How much of your original example code was just illustrative?
Will you always be running inside infinite for loops like the one shown?
Is there one loop where you do this or many?
The first solution that keeps coming to mind is a "goto" statement since that is all that a "break" statement really is doing.
Does this code need to run in many compilers or just gcc?
Would it be acceptable to use "for(int quit=1; quit;)" instead of "for(;;)" and then modify your code slightly to accommodate? What about placing the entire body of the for loop into an inline function. Then you could replace your "break" statements with a "return" statement. Something like:

// You may need to pass "bar" and "buzz".
inline int forBody(void)
{
 ...
 // replace "break" with "return(0)"
 ...

 return(1);
}

....

 for(; forBody(););

I just feel like if we better understood what you are trying to accomplish, perhaps the code could be refactored to accomplish the simplicity and elegance that you seek.

Harvey


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux