fork download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6. #include <iterator>
  7. #include <vector>
  8.  
  9. struct StringificationOptions
  10. {
  11. std::string delimiter;
  12. };
  13.  
  14. #define DEFAULT_OPTIONS {}
  15.  
  16. template <typename T, template <typename, class = std::allocator <T> > class ContainerT,
  17. class = typename std::enable_if<std::is_arithmetic<T>::value>::type >
  18. std::string js_stringify(std::string const& name, ContainerT <T> values, StringificationOptions = DEFAULT_OPTIONS)
  19. {
  20. std::stringstream sstr;
  21. sstr << '"'
  22. << name
  23. << "\":["
  24. ;
  25. if (!values.empty())
  26. {
  27. auto bte = values.end();
  28. bte--;
  29. std::copy(values.begin(), bte, std::ostream_iterator<T>(sstr, ","));
  30. sstr << values.back();
  31. }
  32. sstr << ']';
  33. return sstr.str();
  34. }
  35.  
  36. std::string js_stringify(std::string const& name, std::string const& value, StringificationOptions = DEFAULT_OPTIONS, bool as_array = false)
  37. {
  38. if (as_array)
  39. return js_stringify(name, std::vector <std::string::value_type> {value.begin(), value.end()});
  40.  
  41. std::stringstream sstr;
  42. sstr << "\""
  43. << name
  44. << "\":\""
  45. << value
  46. << "\""
  47. ;
  48. return sstr.str();
  49. }
  50.  
  51. int main() {
  52. std::vector <int> c = {1, 2, 3};
  53. std::cout << js_stringify("c", c) << "\n";
  54. return 0;
  55. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘std::string js_stringify(const string&, const string&, StringificationOptions, bool)’:
prog.cpp:39:101: internal compiler error: in create_tmp_var, at gimplify.c:479
         return js_stringify(name, std::vector <std::string::value_type> {value.begin(), value.end()});
                                                                                                     ^
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.8/README.Bugs> for instructions.
Preprocessed source stored into /home/QMpCIc/ccgtR0HW.out file, please attach this to your bugreport.
stdout
Standard output is empty