fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. struct valdef
  5. {
  6. typedef T value;
  7. };
  8.  
  9. template <char... C>
  10. struct string
  11. {
  12. static char const c_str[];
  13. };
  14.  
  15. template <char... C>
  16. char const string<C...>::c_str[] = { C..., 0 };
  17.  
  18. template <typename, typename>
  19. struct append;
  20.  
  21. template <char... C, char... D>
  22. struct append<string<C...>, string<D...>>
  23. : valdef<string<C..., D...>>
  24. {};
  25.  
  26. template <char C, unsigned N>
  27. struct make_string_n
  28. : valdef
  29. <
  30. typename append
  31. <
  32. typename make_string_n<C, N - 1>::value,
  33. string<C>
  34. >::value
  35. >
  36. {};
  37.  
  38. template <char C>
  39. struct make_string_n<C, 0>
  40. : valdef<string<>>
  41. {};
  42.  
  43. template <char C, unsigned Chars, char F, unsigned Fills>
  44. struct make_rhombus_line
  45. : valdef
  46. <
  47. typename append
  48. <
  49. typename append
  50. <
  51. typename make_string_n<F, Fills>::value,
  52. typename make_string_n<C, Chars>::value
  53. >::value,
  54. string<'\n'>
  55. >::value
  56. >
  57. {};
  58.  
  59. template <char C, unsigned Chars, char F, unsigned Fills>
  60. struct rhombus
  61. : valdef
  62. <
  63. typename append
  64. <
  65. typename make_rhombus_line<C, Chars, F, Fills>::value,
  66. typename append
  67. <
  68. typename rhombus<C + 1, Chars + 2, F, Fills - 1>::value,
  69. typename make_rhombus_line<C, Chars, F, Fills>::value
  70. >::value
  71. >::value
  72. >
  73. {};
  74.  
  75. template <char C, unsigned Chars, char F>
  76. struct rhombus<C, Chars, F, 0>
  77. : valdef<typename make_rhombus_line<C, Chars, F, 0>::value>
  78. {};
  79.  
  80. template <char C, char F>
  81. struct make_rhombus
  82. : valdef<typename rhombus<'A', 1, F, C - 'A'>::value>
  83. {};
  84.  
  85. int main()
  86. {
  87. std::cout << make_rhombus<'D', ' '>::value::c_str;
  88. }
Success #stdin #stdout 0s 2828KB
stdin
Standard input is empty
stdout
   A
  BBB
 CCCCC
DDDDDDD
 CCCCC
  BBB
   A