fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. template <typename T>
  5. std::string the_type_helper(T) {
  6. return "not a string";
  7. }
  8.  
  9. template <>
  10. std::string the_type_helper<std::string>(std::string a_variable) {
  11. if (a_variable == "1") {
  12. return "a string containing 1";
  13. } else {
  14. return "just a string";
  15. }
  16. }
  17.  
  18. template <typename T>
  19. std::string the_type(T a_variable) {
  20. std::string r_string;
  21.  
  22. r_string = the_type_helper<T>(a_variable);
  23.  
  24. return r_string;
  25. }
  26.  
  27. int main()
  28. {
  29. int i = 1;
  30. std::cout << the_type(i) << std::endl;
  31.  
  32. char c[] = "121345";
  33. std::cout << the_type(c) << std::endl;
  34.  
  35. std::string s = "121345";
  36. std::cout << the_type(s) << std::endl;
  37.  
  38. s = "1";
  39. std::cout << the_type(s) << std::endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 4552KB
stdin
Standard input is empty
stdout
not a string
not a string
just a string
a string containing 1