From: Scott L. <sl...@cs...> - 2002-04-02 22:15:02
|
Ah, I see. And the do .. while(0) part is so that stuff like if(a) DO_STUFF(1); else DO_STUFF(2); works as expected. Is there a reason they aren't inline functions? - Scott > So that they work as you'd expect when preceded by an if clause-- they > need to be a single statement (which many of them happen to already be, > but if the macro definition got any more complicated, they wouldn't > be). E.g., consider: > > #define DO_STUFF(x) \ > do something; > do something else; > > being used as: > > if (...) > DO_STUFF(x) > > it'll expand into: > > if (...) > { > do something; > } > do something else; > > I've added braces to stress the problem. The do/while(0) makes it work > as the user of the macro probably expected, w/o requiring her to > remember to use braces everytime she uses DO_STUFF. > > I think the GNU C programming standards mention this coding style, but I > don't have a reference handy. > > Thanks, > Greg > > > Scott Lenser <sl...@cs...> writes: > > > Greg, > > > > In the validate macros such as: > > > > #define VALIDATE_WIN_USE_CONTEXT(win) \ > > do { if ((win = ensure_valid(win,1,FUNC_NAME, SCM_BOOL_T, SCM_BOOL_F)) == > > SCM_BOOL_F) \ > > return SCM_BOOL_F; } while (0) > > > > Why do they all have a do { } while(0) around the code? > > > > Thanks. > > > > - Scott > > _______________________________________________ > Scwm-devs mailing list > Scw...@li... > https://lists.sourceforge.net/lists/listinfo/scwm-devs |