Created
March 31, 2016 16:38
-
-
Save steveobbayi/6514ff69d339b70880f4f442f1101e7f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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