Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. /*
  2. * This is just some mickey-mouse code to demonstrate a point about static
  3. * dispatch.
  4. *
  5. * print_line2() is the public interface.
  6. */
  7.  
  8. #include <iostream>
  9.  
  10. using namespace std;
  11.  
  12. enum line_style {line_default, dotted, dashed, solid};
  13. enum end_style {end_default, bracket, circle};
  14.  
  15. template <line_style L=line_default>
  16. void print_line()
  17. {
  18. cout << "########";
  19. }
  20.  
  21. template <>
  22. void print_line<dotted>()
  23. {
  24. cout << "........";
  25. }
  26.  
  27. template <>
  28. void print_line<dashed>()
  29. {
  30. cout << "--------";
  31. }
  32.  
  33. template <end_style E=end_default>
  34. struct print_end
  35. {
  36. static
  37. void print_left()
  38. {
  39. cout << " ";
  40. }
  41.  
  42. static
  43. void print_right()
  44. {
  45. print_left();
  46. }
  47. };
  48.  
  49.  
  50. template <>
  51. struct print_end<bracket>
  52. {
  53. static
  54. void print_left()
  55. {
  56. cout << "<";
  57. }
  58.  
  59. static
  60. void print_right()
  61. {
  62. cout << ">";
  63. }
  64. };
  65.  
  66. template <>
  67. struct print_end<circle>
  68. {
  69. static
  70. void print_left()
  71. {
  72. cout << "o";
  73. }
  74.  
  75. static
  76. void print_right()
  77. {
  78. print_left();
  79. }
  80. };
  81.  
  82. template <line_style L=line_default, end_style E=end_default>
  83. void print_line2()
  84. {
  85. print_end<E>::print_left();
  86. print_line<L>();
  87. print_end<E>::print_right();
  88. cout << endl;
  89. }
  90.  
  91. int main(int, char **)
  92. {
  93. print_line2();
  94. print_line2<dashed>();
  95. print_line2<dotted>();
  96. print_line2<dotted, bracket>();
  97. print_line2<dashed, circle>();
  98. // print_line2<circle>(); // Does not compile, error message is:
  99. // error: no matching function for call to ‘print_line2()’
  100. // error: could not convert template argument ‘circle’ to ‘line_style’
  101.  
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement