Swap in C++

1 minute read Published: 2024-11-11

How can you not be romantic about XOR?

I posted this gist 13 years ago... Know, it's time to think about it.

// Swap with no extra memory
template <class T>
void swap(T& a, T& b) {
    a ^= b;
    b ^= a;
    a ^= b;
}