On Wed, 15 Nov 2000, Doug Stalker wrote:
> void swap (int x, int y) { int tmp; tmp=x; x=y; y=tmp }
>
> #define swap(a,b) { a^=b;b^=a;a^=b }
But we are talking C++ here:
inline void swapper(int& x, int& y) { x^=y; y^=x; x^=y; }
Using C++ as a better C, we have the best of both
of the above C solutions: 3 XORs only, and safe function calling.
Remember: #defines are evil & dangerous.
Try this on for size: swap(x++,--y)
At best it will give you bizarre compile time errors.
At worst it will compile and give you crap.
Whereas swapper(x++,--y) will warn you that the compiler
is taking a reference to a (discarded) temporary variable
(a towrst) or disallow taking a reference to an expression
(at best).
For a real twister, try this: swap(c,i) where c is char and i is int.
It will compile and trash some of the bits in i. Whereas C++
says "no way: cannot pass a char via an int reference".
--
Rick Welykochy || Praxis Services
"Tired of being a crash test dummy for Microsoft? Try Linux"
--
SLUG - Sydney Linux User Group Mailing List - http://slug.org.au/
More Info: http://slug.org.au/lists/listinfo/slug