fork download
  1. #include <iostream>
  2. #include <cstdarg>
  3.  
  4. using namespace std;
  5.  
  6. enum types
  7. {
  8. INT,
  9. DOUBLE,
  10. CHAR,
  11. STRING
  12. };
  13.  
  14. struct multitype
  15. {
  16. types type;
  17.  
  18. union
  19. {
  20. int i;
  21. double d;
  22. char c;
  23. const char *s;
  24. } val;
  25.  
  26.  
  27. };
  28.  
  29. multitype mt(int i)
  30. {
  31. multitype res;
  32. res.type = INT;
  33. res.val.i = i;
  34. return res;
  35. }
  36.  
  37. multitype mt(double d)
  38. {
  39. multitype res;
  40. res.type = DOUBLE;
  41. res.val.d = d;
  42. return res;
  43. }
  44.  
  45. multitype mt(char c)
  46. {
  47. multitype res;
  48. res.type = CHAR;
  49. res.val.c = c;
  50. return res;
  51. }
  52.  
  53. multitype mt(const char *s)
  54. {
  55. multitype res;
  56. res.type = STRING;
  57. res.val.s = s;
  58. return res;
  59. }
  60.  
  61. void print(int n, ...)
  62. {
  63. va_list ap;
  64.  
  65. va_start(ap, n);
  66.  
  67. for (int i = 0; i < n; i++)
  68. {
  69. multitype x(va_arg(ap, multitype));
  70.  
  71. switch (x.type)
  72. {
  73. case INT:
  74. cout << x.val.i << endl;
  75. break;
  76. case DOUBLE:
  77. cout << x.val.d << endl;
  78. break;
  79. case CHAR:
  80. cout << x.val.c << endl;
  81. break;
  82. case STRING:
  83. cout << x.val.s << endl;
  84. break;
  85. }
  86. }
  87.  
  88. va_end(ap);
  89. }
  90.  
  91. int main()
  92. {
  93. print(4, mt(2), mt(4.2), mt('a'), mt("Hello"));
  94. }
Success #stdin #stdout 0.01s 2680KB
stdin
Standard input is empty
stdout
2
4.2
a
Hello