#include #include using namespace std; #define SWAP(T, a, b) {const T temp = (a); (a) = (b); (b) = temp;} int main() { int i = 10; int j = 20; SWAP(int, i, j); //will compile cout << "i == " << i << ", j == " << j << "\n"; int temp = 30; //SWAP(int, i, temp); //won't compile size_t a[] = {10, 20, 30}; size_t k = 2; SWAP(size_t, k, a[k]); //undefined behavior cout << "k == " << k << ", a[2] == " << a[2] << "\n"; const bool b = true; int x = 10, y = 20, z = 30; SWAP(int, b ? x : y, z); //swap x and z cout << "x == " << x << ", y == " << y << ", z == " << z << "\n"; return EXIT_SUCCESS; }