fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Console
  5. {
  6. public:
  7. template<typename ...Args>
  8. static bool WriteLine(const std::string& format, Args&&... args)
  9. {
  10. bool success = write(format.c_str(), std::forward<Args>(args)...);
  11. std::cout << std::endl;
  12. return success;
  13. }
  14.  
  15. template <typename ...Args>
  16. static bool Write(const std::string& format, Args&&... args)
  17. {
  18. return write(format.c_str(), std::forward<Args>(args)...);
  19. }
  20.  
  21. template<typename T>
  22. static void WriteLine(T&& value)
  23. {
  24. std::cout << std::forward<T>(value) << std::endl;
  25. }
  26.  
  27. template<typename T>
  28. static void Write(T&& value)
  29. {
  30. std::cout << std::forward<T>(value);
  31. }
  32.  
  33. private:
  34. template<typename ...Args>
  35. static bool write(const char * format, Args&&... args)
  36. {
  37. while (*format)
  38. {
  39. if (*format == '{')
  40. {
  41. bool found_closing_brace = false;
  42. short value_position = -1;
  43.  
  44. while (*(++format))
  45. {
  46. if (*format == '}')
  47. {
  48. if (value_position == -1)
  49. return false; // {} is not allowed.
  50.  
  51. write_value(value_position, 0, std::forward<Args>(args)...);
  52. found_closing_brace = true;
  53. break;
  54. }
  55. else
  56. {
  57. if (value_position >= 10)
  58. return false; //Only range {0 ~ 99} is allowed.
  59.  
  60. if (value_position == -1)
  61. value_position = *format - '0';
  62. else
  63. value_position = (value_position * 10) + (*format - '0');
  64. }
  65. }
  66.  
  67. if (!found_closing_brace)
  68. return false;
  69.  
  70. // Continue back to the main loop. This is required.
  71. // We need to process the next character, because it could be a '\0' or a '{'
  72. format++;
  73. continue;
  74. }
  75.  
  76. std::cout << *format;
  77. format++;
  78. }
  79.  
  80. return true;
  81. }
  82.  
  83. template<typename T, typename ...Args>
  84. static void write_value(int x, int i, T&& value, Args&&... values)
  85. {
  86. if (i == x)
  87. std::cout << std::forward<T>(value);
  88. else
  89. write_value(x, ++i, std::forward<Args>(values)...);
  90. }
  91.  
  92. static void write_value(int x, int i) { }
  93. };
  94.  
  95. int main()
  96. {
  97. Console::WriteLine("Big {11} Bang {0} Theory {6} [{11}, {12}]",
  98.  
  99. "Zero", "One", "Two", "Three", "Four", "Five",
  100. "Six", "Seven", 8, "Nine", "Teen", 11, 12.5f);
  101.  
  102. // your code goes here
  103. return 0;
  104. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Big 11 Bang Zero Theory Six [11, 12.5]