swap (a, b) suggestion

Eric Buddington eric@sparrow.vgernet.net
Sat Jan 3 15:11:00 GMT 1998


Greetings.

I am just learning about STL, and have found a possible improvement
for the swap() template in include/g++/stl_algobase.h.  The template
here is straightforward:

template <class T>
inline void swap(T& a, T& b) {
  T tmp = a;
  a = b;
  b = tmp;
}

For integer values, the following works, and is significantly faster:

template <class T>
inline void swap(T& a, T& b) {
  a ^= b;
  b ^= a;
  a ^= b;
}

In my own code, this change sped up a quicksort significantly.
Explicit versions for char, short, int, and long (and their unsigned
counterparts) alongside the template would do the trick.

I'll be interested to hear any comments. Thanks for your collective
work - egcs has really saved my butt development-wise.

Eric Buddington



More information about the Gcc mailing list