swap(x,y)

T. William Wells bill at twwells.com
Sat Sep 2 01:04:50 AEST 1989


In article <1545 at l.cc.purdue.edu> cik at l.cc.purdue.edu (Herman Rubin) writes:
: In article <14479 at haddock.ima.isc.com>, karl at haddock.ima.isc.com (Karl Heuer) writes:
: > In article <230 at crdos1.crd.ge.COM> davidsen at crdos1.UUCP (bill davidsen) writes:
: > >However, there are reasons for doing tricks such as the XOR thing.  They
: > >enable me to write a macro which does the swap for any type.
: >
: > Only for integral types.  It won't work on float, pointer, struct, etc.
:
: If one adds a pseudo-op, say use (I will not object to a more appropriate
: term) so that (use int)x means treat x as type int no matter what it was,

Try: *(int *)&x. If you are feeling particularly nonportable today.

: if possible, then it will work on float and pointer, and even swap between,
: say, float and pointer.

No it won't. At the very least, you would have had to define it as
"as if it were an integer of the appropriate size" in which case,
while the construct will now do something that can't be done quite as
easily, it doesn't add any real utility to the language.

Such a construct would be just as nonportable as the cast above.

Moreoever, you clearly have forgotten, or never learned, that floats,
pointers, ints and most other things have sizes that are not
necessarily equal.

:                          If this is modified to allow multiple-word types,
: it could handle double and struct.

Why bother? The only advantage this silly construct has over the cast
above is its polymorphism; and given the lack of support for that
anywhere else in the language, it is pointless. (No, don't tell me
the language should have all those features. It shouldn't. Why?
Because that would make it a different language: C++.)

: This is another example of keeping the tools from the programmer.

This is yet another example of Mr. Rubin wishing for the moon.

To repeat for the hundredth time: a programming language can not have
all features nor can it be everything to everyone. There are good
theoretical reasons for this and decades of experience to drive home
the point.

Mr. Rubin? Shut up. Your wishes are a waste of our time. You have,
time and again, demonstrated your fuzzy thinking and your ignorance
of the most elementary considerations. You are an idiot. And a fool.

Get lost. Develop some humility. Learn this language you keep
carping, ignorantly, about, and then _maybe_ you will have something
useful to say.

Followups have been directed to alt.flame.

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill at twwells.com



More information about the Comp.lang.c mailing list