Skip to content

Instantly share code, notes, and snippets.

@ashgti
Created April 4, 2012 17:02
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 ashgti/2303864 to your computer and use it in GitHub Desktop.
Save ashgti/2303864 to your computer and use it in GitHub Desktop.
C++ Templates with default params
Type is... int
Type is... char
Type is... float
Type is... double
Type is... long
Type is... short
Type is... int
Type is... double
#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