fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. template <class T>
  6. struct Student {
  7. Student(T const &);
  8. void calcDifference(int idx, T const & val1, T const & val2);
  9. }; // Student
  10.  
  11. template <class T>
  12. void Student<T>::calcDifference(int idx, T const & val1, T const & val2)
  13. {
  14. T difference = val1 - val2;
  15. cout << "Difference values: " << difference << endl;
  16. }
  17.  
  18. template <>
  19. void Student<string>::calcDifference(
  20. int idx, string const & val1, string const & val2)
  21. {
  22. cout << "Computing string difference (" << val1 << " - " << val2
  23. << ") is undefined!" << endl;
  24. }
  25.  
  26. template <class T>
  27. Student<T>::Student(T const & t)
  28. {
  29. cout << "Constructor called for " << t << endl;
  30. }
  31.  
  32. int main(int argc, const char * argv[])
  33. {
  34. Student<int> iStudent(10);
  35. Student<string> oStudent("hi");
  36.  
  37. iStudent.calcDifference(1, 12, 10);
  38. oStudent.calcDifference(1, "aaa", "aa");
  39. }
  40.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
Constructor called for 10
Constructor called for hi
Difference values: 2
Computing string difference (aaa - aa) is undefined!