类型安全的变参函数any2string(),可用来替代类型不安全的snprintf()

简介: any2string.sh用来生成any2string.h和test_any2string.cpp两个文件:https://github.com/eyjian/mooon/blob/master/common_library/include/util/any2string.
any2string.sh用来生成any2string.h和test_any2string.cpp两个文件:
https://github.com/eyjian/mooon/blob/master/common_library/include/util/any2string.sh

any2string.h提供了一个类型安全的变参函数any2string(),可用来替代类型不安全的snprintf()等:
https://github.com/eyjian/mooon/blob/master/common_library/include/util/any2string.h

test_any2string.cpp用来测试any2string.h
https://github.com/eyjian/mooon/blob/master/common_library/include/util/test_any2string.cpp

any2string.h:
  1. // Write by yijian on 2014/12/27
  2. // DO NOT
  3. // this header file is auto generated by any2string.sh
  4. // edit any2string.sh if necessary
  5. //
  6. // 类型安全的变参函数,可用来替代类型不安全的sprintf()和snprintf()等函数
  7. // 使用示例1: std::string str = any2string(20141227);
  8. // 使用示例2: std::string str = any2string(20141227, "22:07:10");
  9. // 使用示例3: std::string str = any2string(1, "2", '3', std::string("4"));
  10. #ifndef MOOON_UTIL_ANY2STRING_H
  11. #define MOOON_UTIL_ANY2STRING_H
  12. #include string>
  13. #include sstream>

  14. template typename T0>
  15. inline std::string any2string(const T0& t0)
  16. {
  17.     std::stringstream ss;
  18.     ss t0;
  19.     return ss.str();
  20. }

  21. template typename T0, typename T1>
  22. inline std::string any2string(const T0& t0, const T1& t1)
  23. {
  24.     std::stringstream ss;
  25.     ss t0 t1;
  26.     return ss.str();
  27. }

  28. template typename T0, typename T1, typename T2>
  29. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2)
  30. {
  31.     std::stringstream ss;
  32.     ss t0 t1 t2;
  33.     return ss.str();
  34. }

  35. template typename T0, typename T1, typename T2, typename T3>
  36. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3)
  37. {
  38.     std::stringstream ss;
  39.     ss t0 t1 t2 t3;
  40.     return ss.str();
  41. }

  42. template typename T0, typename T1, typename T2, typename T3, typename T4>
  43. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4)
  44. {
  45.     std::stringstream ss;
  46.     ss t0 t1 t2 t3 t4;
  47.     return ss.str();
  48. }

  49. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5>
  50. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5)
  51. {
  52.     std::stringstream ss;
  53.     ss t0 t1 t2 t3 t4 t5;
  54.     return ss.str();
  55. }

  56. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
  57. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6)
  58. {
  59.     std::stringstream ss;
  60.     ss t0 t1 t2 t3 t4 t5 t6;
  61.     return ss.str();
  62. }

  63. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7>
  64. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7)
  65. {
  66.     std::stringstream ss;
  67.     ss t0 t1 t2 t3 t4 t5 t6 t7;
  68.     return ss.str();
  69. }

  70. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8>
  71. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8)
  72. {
  73.     std::stringstream ss;
  74.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8;
  75.     return ss.str();
  76. }

  77. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9>
  78. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9)
  79. {
  80.     std::stringstream ss;
  81.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9;
  82.     return ss.str();
  83. }

  84. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10>
  85. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10)
  86. {
  87.     std::stringstream ss;
  88.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10;
  89.     return ss.str();
  90. }

  91. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11>
  92. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11)
  93. {
  94.     std::stringstream ss;
  95.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11;
  96.     return ss.str();
  97. }

  98. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12>
  99. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12)
  100. {
  101.     std::stringstream ss;
  102.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12;
  103.     return ss.str();
  104. }

  105. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13>
  106. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13)
  107. {
  108.     std::stringstream ss;
  109.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13;
  110.     return ss.str();
  111. }

  112. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14>
  113. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14)
  114. {
  115.     std::stringstream ss;
  116.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14;
  117.     return ss.str();
  118. }

  119. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15>
  120. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15)
  121. {
  122.     std::stringstream ss;
  123.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15;
  124.     return ss.str();
  125. }

  126. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16>
  127. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16)
  128. {
  129.     std::stringstream ss;
  130.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16;
  131.     return ss.str();
  132. }

  133. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17>
  134. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17)
  135. {
  136.     std::stringstream ss;
  137.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17;
  138.     return ss.str();
  139. }

  140. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18>
  141. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18)
  142. {
  143.     std::stringstream ss;
  144.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18;
  145.     return ss.str();
  146. }

  147. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19>
  148. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19)
  149. {
  150.     std::stringstream ss;
  151.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19;
  152.     return ss.str();
  153. }

  154. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20>
  155. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20)
  156. {
  157.     std::stringstream ss;
  158.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20;
  159.     return ss.str();
  160. }

  161. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21>
  162. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21)
  163. {
  164.     std::stringstream ss;
  165.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21;
  166.     return ss.str();
  167. }

  168. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22>
  169. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22)
  170. {
  171.     std::stringstream ss;
  172.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22;
  173.     return ss.str();
  174. }

  175. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23>
  176. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23)
  177. {
  178.     std::stringstream ss;
  179.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23;
  180.     return ss.str();
  181. }

  182. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23, typename T24>
  183. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23, const T24& t24)
  184. {
  185.     std::stringstream ss;
  186.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24;
  187.     return ss.str();
  188. }

  189. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23, typename T24, typename T25>
  190. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23, const T24& t24, const T25& t25)
  191. {
  192.     std::stringstream ss;
  193.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24 t25;
  194.     return ss.str();
  195. }

  196. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23, typename T24, typename T25, typename T26>
  197. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23, const T24& t24, const T25& t25, const T26& t26)
  198. {
  199.     std::stringstream ss;
  200.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24 t25 t26;
  201.     return ss.str();
  202. }

  203. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23, typename T24, typename T25, typename T26, typename T27>
  204. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23, const T24& t24, const T25& t25, const T26& t26, const T27& t27)
  205. {
  206.     std::stringstream ss;
  207.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24 t25 t26 t27;
  208.     return ss.str();
  209. }

  210. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23, typename T24, typename T25, typename T26, typename T27, typename T28>
  211. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23, const T24& t24, const T25& t25, const T26& t26, const T27& t27, const T28& t28)
  212. {
  213.     std::stringstream ss;
  214.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24 t25 t26 t27 t28;
  215.     return ss.str();
  216. }

  217. template typename T0, typename T1, typename T2, typename T3, typename T4, typename T5, typename T6, typename T7, typename T8, typename T9, typename T10, typename T11, typename T12, typename T13, typename T14, typename T15, typename T16, typename T17, typename T18, typename T19, typename T20, typename T21, typename T22, typename T23, typename T24, typename T25, typename T26, typename T27, typename T28, typename T29>
  218. inline std::string any2string(const T0& t0, const T1& t1, const T2& t2, const T3& t3, const T4& t4, const T5& t5, const T6& t6, const T7& t7, const T8& t8, const T9& t9, const T10& t10, const T11& t11, const T12& t12, const T13& t13, const T14& t14, const T15& t15, const T16& t16, const T17& t17, const T18& t18, const T19& t19, const T20& t20, const T21& t21, const T22& t22, const T23& t23, const T24& t24, const T25& t25, const T26& t26, const T27& t27, const T28& t28, const T29& t29)
  219. {
  220.     std::stringstream ss;
  221.     ss t0 t1 t2 t3 t4 t5 t6 t7 t8 t9 t10 t11 t12 t13 t14 t15 t16 t17 t18 t19 t20 t21 t22 t23 t24 t25 t26 t27 t28 t29;
  222.     return ss.str();
  223. }

  224. #endif // MOOON_UTIL_ANY2STRING_H

相关文章
|
1月前
|
存储 程序员 C++
C++ 标准库类型之string
C++ 标准库类型之string
23 0
|
2月前
|
Linux
vs2019测试sizeof(string)的长度是28
vs2019测试sizeof(string)的长度是28
25 0
|
6月前
|
算法 C语言 C++
string和自动推断类型
string和自动推断类型
|
6月前
|
Go
golang 中string和int类型相互转换
golang 中string和int类型相互转换
75 0
|
8月前
编写一个程序,链接两个字符串字面常量,将结果保存在一个动态分配的char数组中,重写这个程序,连接两个标准string对象
编写一个程序,链接两个字符串字面常量,将结果保存在一个动态分配的char数组中,重写这个程序,连接两个标准string对象
|
8月前
模拟实现字符串函数以及内存函数(均包含在头文件“string.h“中)
①不限字符个数型(strlen, strcpy, strcmp, strcat,strstr) ②限字符个数型(strncpy, strcmp, strncat) ③内存型(memcpy, memmove, memset)
45 0
|
9月前
|
C++
c++ 中标准库类型 string 详解
准库类型string 表示可变长的字符序列,使用string 类型必须首先包含string 头文件。string 定义在命名空间std 中。
91 0
|
9月前
|
JSON NoSQL Redis
关于Redis-存Long取Integer类型转换错误的问题;String对象被转义的问题
关于Redis-存Long取Integer类型转换错误的问题;String对象被转义的问题
333 0
json.Unmarshal() 反序列化字节流到 interface{} 对象,字段 int/int64 类型出现精度丢失
json.Unmarshal() 反序列化字节流到 interface{} 对象,字段 int/int64 类型出现精度丢失
【C++】string和int类型相互转换
【C++】string和int类型相互转换
【C++】string和int类型相互转换