fork download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. template<typename T>
  6. std::string to_string(const T& obj)
  7. {
  8. std::ostringstream s;
  9. s << obj;
  10. return s.str();
  11. }
  12.  
  13. struct MyType
  14. {
  15. int x;
  16. double y;
  17. std::string name;
  18. };
  19.  
  20. std::ostream& operator<<(std::ostream& ostream, const MyType& type)
  21. {
  22. ostream << "x= " << type.x << '\n';
  23. ostream << "y= " << type.y << '\n';
  24. ostream << "name= " << type.name;
  25. return ostream;
  26. }
  27.  
  28. int main(int argc, char* argv[])
  29. {
  30. std::string temp = to_string(1) + " " + to_string(50.4) + "\n" + to_string(MyType { 10, 20, "Test"});
  31.  
  32. std::cout << temp;
  33. return 0;
  34. }
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
1 50.4
x= 10
y= 20
name= Test