invertire 2 variabili a=b b=a

si può fare usando un pò di matematica e facendo attenzione agli overflow:

void swap(int &x, int &y)
{
    x -= y;
    y += x;         // y gets the original value of x
    x = (y - x);    // x gets the original value of y
}

enjoy

edit da: http://devmaster.net/forums/topic/266-swapping-two-variables-without-using-a-temp-var/

fighissima la

void swap2(int &x, int &y)
{
        x ^= y ^= x ^= y;
}