Monday, December 04, 2006

Swapping values without temporary variable

Just ran across it while browsing for something else, and I think it's pretty cool... in a useless kind of way! (Also I think I saw this once before, but can't remember when...) template <class T> inline void swap (T& x, T& y) { x = x ^ y; y = x ^ y; x = x ^ y; } Of course, it will only work with primitive types, for which operator^ is well defined!

No comments: