/**
 * A very brief example of a template function 
 * (that happens to use references).
 * 
 * In a template function, the data type of the parameters
 * becomes a sort of parameter itself.
 *
 * Author: Zach Tomaszewski 
 */
#include <iostream>
#include <string>

using std::cout;
using std::endl;
using std::string;

//beware: there is already a std::swap function in <string>
template <class C> void swap(C& a, C& b);

int main() {

  //swapping ints
  cout << "ints:" << endl;
  int small = 4;
  int large = 10;
  cout << "before swap: " << small << "  " << large << endl;
  swap(small, large);
  cout << "after swap: " << small << "  " << large << endl;
  cout << endl;

  //swapping ints
  cout << "doubles:" << endl;
  double little = 4.0;
  double big = 11.5;
  cout << "before swap: " << little << "  " << big << endl;
  swap(little, big);
  cout << "after swap: " << little << "  " << big << endl;
  cout << endl;
  
  //swapping string
  cout << "strings:" << endl;
  string one = "short";
  string two = "longer";
  cout << "before swap: " << one << "  " << two << endl;
  swap(one, two);
  cout << "after swap: " << one << "  " << two << endl;  
  
  return 0;
}

/*
 * Swaps two objects of the same type, regardless of type.
 */
template <class C> void swap(C& a, C&b) {
  C temp = a;
  a = b;
  b = temp;
}