Swapping general objects

Header file swap.h

In many programs, it's necessary to swap the values of two variables. This entails the use of a temporary variable, e.g.

double x, y;
...
// now we want to swap the values of x and y
double tmp;
tmp = x; x = y; y = tmp;
This pattern of coding must be repeated each time a swap is performed.

The `swap' method is a template to swap objects of the same type in a single statement (from the programmer's perspective). It operates by creating the temporary copy and swapping exactly as above, but this is hidden in the coding and looks much neater. Example of use:

#include <swap.h>
...
Vector a, b; // objects of some general class, e.g. `vector' here
...
Swap(a,b); // swaps values

`swap' should be used with care if operator `=' doesn't copy the values of members of the class to be swapped to new stores, i.e. if `=' has been overloaded in a way which is not compatible with this library. It could also result in memory leakage if the class to be swapped doesn't allocate and deallocate storage symmetrically (i.e. if all storage allocated when the temporary copy is created is not returned when the it's destroyed).