Path: utzoo!utgpu!water!watmath!clyde!rutgers!husc6!yale!bunker!garys From: garys@bunker.UUCP (Gary M. Samuelson) Newsgroups: comp.lang.c Subject: Re: comma operator Message-ID: <3203@bunker.UUCP> Date: 19 Jan 88 14:57:50 GMT References: <3819@sigi.Colorado.EDU> <5080013@hpfcdc.HP.COM> <7120@brl-smoke.ARPA> <3887@sigi.Colorado.EDU> Reply-To: garys@bunker.UUCP (Gary M. Samuelson) Organization: Bunker Ramo, an Olivetti Company, Shelton, Ct Lines: 33 In article <3887@sigi.Colorado.EDU> swarbric@tramp.Colorado.EDU (SWARBRICK FRANCIS JOHN) writes: >In article <7120@brl-smoke.ARPA> gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) writes: >>By the way, I don't think there IS a good way to write a generic swap >>macro. Didn't we go over this about a year ago? I don't think it was that long ago. >#define swap(a,b) ((a) = ((b) = ((a) = (a) ^ (b)) ^ (b)) ^ (a)) > >Try that, and tell me what you think. A super-genius friend of mine figured >it out. He says it will work for anything (int, char *, etc.). But it >sure boggles my mind... Who told him he was a genius? Self-proclaimed, no doubt. How about float or double? Structures? Suppose either a or b have side effects? (e.g., swap( *x++, *y++ )) What is the value of a macro which performs 6 operations, instead of the three I want? (t = a; a = b; b = t) Suppose a and b are aliases? How is the exclusive-or of two pointers defined? (It isn't, at least not portably). Yes, it's a clever macro. Yes, under some conditions it does the right thing. I even know why it works. But I wouldn't use it; the straightforward approach is much better. So it takes another variable; so what? Are people afraid we'll run out? My first maxim of programming is this: Code will be read more than written; therefore it is more important to be easily read than easily written. Gary Samuelson