fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. template<typename T>
  6. struct Test {
  7. T value;
  8. Test(std::string);
  9. };
  10.  
  11. template<>
  12. inline Test<std::string>::Test(std::string str) {
  13. value = str;
  14. }
  15.  
  16. template<typename T>
  17. inline Test<T>::Test(std::string str) {
  18. std::stringstream ss;
  19. ss.str(str);
  20. ss >> value;
  21. }
  22.  
  23. int main() {
  24. Test<int> t1{"42"};
  25. Test<double> t2{"3.14159"};
  26. Test<std::string> t3{"Hello world"};
  27.  
  28. std::cout
  29. << t1.value << std::endl
  30. << t2.value << std::endl
  31. << t3.value << std::endl;
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
42
3.14159
Hello world