Skip to content

Instantly share code, notes, and snippets.

@praveendhinwa
Created September 22, 2013 13:30
Show Gist options
  • Select an option

  • Save praveendhinwa/6659912 to your computer and use it in GitHub Desktop.

Select an option

Save praveendhinwa/6659912 to your computer and use it in GitHub Desktop.
template_specialization.cpp
#include <bits/stdc++.h>
using namespace std;
template <class T>
class SquareIt
{
public:
T x;
SquareIt ()
{
}
SquareIt (T _x)
{
x = _x;
}
T getResult ()
{
return x * x;
}
};
template <>
class SquareIt <string>
{
public:
string x;
SquareIt ()
{
}
SquareIt (string _x)
{
x = _x;
}
string getResult ()
{
return x + x;
}
};
int main()
{
SquareIt <int> sq1 (5);
cout << sq1.getResult() << endl;
SquareIt <string> sq2 ("abc");
cout << sq2.getResult() << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment