// inline function: reduce function call overhead - especially for small // functions. The qualifier "inline" advises the compiler to generate a // copy of the function's code in place to avoid a function call. The // tradeoff is that multiple copies of the function code are inserted in // the program rather than having a single copy of the function to which // control is passed each time the function is called. But the compiler // can ignore the inline qualifier and typically does so for all but small // functions. Any change to an inline function requires all clients of the // function to be recompiled. #include template inline void swap(T& a, T& b) { T temp = a; a = b; b = temp; } template inline void swap2(T* a, T* b) { T temp = *a; *a = *b; *b = temp; } main( ) { int A[2] = {0, 1}; cout << "before swap, A[0], A[1] = " << A[0] << "," << A[1] << endl; swap(A[0], A[1]); cout << "after swap, A[0], A[1] = " << A[0] << "," << A[1] << endl; cout << endl; cout << "using swap2 " << endl; cout << "before swap2, A[0], A[1] = " << A[0] << "," << A[1] << endl; swap2(&A[0], &A[1]); cout << "after swap2, A[0], A[1] = " << A[0] << "," << A[1] << endl; getchar(); return 0; }