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