Skip to content

Instantly share code, notes, and snippets.

@steveobbayi
Created March 31, 2016 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveobbayi/6514ff69d339b70880f4f442f1101e7f to your computer and use it in GitHub Desktop.
Save steveobbayi/6514ff69d339b70880f4f442f1101e7f to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
template <typename T> // add this line
T squareThis(T x){ // replace all occurrences of the type with "T"
return x*x;
}
int main(){
// specify the type you want to handle in angle brackets
cout << squareThis<int>(2) << endl; // returns 4
cout << squareThis<double>(2.5) << endl; // returns 6.25
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment