Created
April 4, 2012 17:02
-
-
Save ashgti/2303864 to your computer and use it in GitHub Desktop.
C++ Templates with default params
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
Type is... int | |
Type is... char | |
Type is... float | |
Type is... double | |
Type is... long | |
Type is... short | |
Type is... int | |
Type is... double |
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> | |
#include <string> | |
using std::cout; | |
using std::endl; | |
using std::string; | |
void printType(double) { | |
cout << "double"; | |
} | |
void printType(float) { | |
cout << "float"; | |
} | |
void printType(short) { | |
cout << "short"; | |
} | |
void printType(int) { | |
cout << "int"; | |
} | |
void printType(long) { | |
cout << "long"; | |
} | |
void printType(char) { | |
cout << "char"; | |
} | |
void printType(long long) { | |
cout << "long long"; | |
} | |
template<typename T> | |
void foobar(T t = 0) { | |
cout << "Type is... "; | |
printType(t); | |
cout << endl; | |
} | |
int main() { | |
foobar(2); | |
foobar('a'); | |
foobar(2.3f); | |
foobar(2.3); | |
foobar(2l); | |
foobar((short)2); | |
foobar<int>(); | |
foobar<double>(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment