Path: utzoo!utgpu!jarvis.csri.toronto.edu!rutgers!njin!princeton!udel!haven!purdue!tut.cis.ohio-state.edu!cs.utexas.edu!yale!cmcl2!stealth.acf.nyu.edu!brnstnd From: brnstnd@stealth.acf.nyu.edu Newsgroups: comp.lang.c Subject: Is this swap() macro correct? Message-ID: <21068@stealth.acf.nyu.edu> Date: 17 Jan 90 09:34:05 GMT Reply-To: brnstnd@stealth.acf.nyu.edu (Dan Bernstein) Distribution: usa Organization: IR Lines: 21 Say swap() is defined as #define block do { #define endblock } while(0) #define swap(x,y,typ) block typ *_MV_x = x; typ *_MV_y = y; typ tmp;\ tmp = *_MV_x; *_MV_x = *_MV_y; *_MV_y = tmp; endblock Now constructions like swap(f++,g++,real) work and are faster than a function call. Most optimizers are even smart enough to realize that the do { } while(0) is purely syntactic. swap() does not work within an expression, and the macro will fail miserably if _MV_whatever is another macro. Other than that, does swap() have any side effects or other problems? Is anyone interested in a program that converts functions to these inline functions automatically? (Don't bother saying ``if typ is a struct or union and your compiler isn't ANSI then swap() will fail.'' I'm thinking about the problems of the conversion from function to macro, not of swap() in particular.) ---Dan