Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * This is just some mickey-mouse code to demonstrate a point about static
- * dispatch.
- *
- * print_line2() is the public interface.
- */
- #include <iostream>
- using namespace std;
- enum line_style {line_default, dotted, dashed, solid};
- enum end_style {end_default, bracket, circle};
- template <line_style L=line_default>
- void print_line()
- {
- cout << "########";
- }
- template <>
- void print_line<dotted>()
- {
- cout << "........";
- }
- template <>
- void print_line<dashed>()
- {
- cout << "--------";
- }
- template <end_style E=end_default>
- struct print_end
- {
- static
- void print_left()
- {
- cout << " ";
- }
- static
- void print_right()
- {
- print_left();
- }
- };
- template <>
- struct print_end<bracket>
- {
- static
- void print_left()
- {
- cout << "<";
- }
- static
- void print_right()
- {
- cout << ">";
- }
- };
- template <>
- struct print_end<circle>
- {
- static
- void print_left()
- {
- cout << "o";
- }
- static
- void print_right()
- {
- print_left();
- }
- };
- template <line_style L=line_default, end_style E=end_default>
- void print_line2()
- {
- print_end<E>::print_left();
- print_line<L>();
- print_end<E>::print_right();
- cout << endl;
- }
- int main(int, char **)
- {
- print_line2();
- print_line2<dashed>();
- print_line2<dotted>();
- print_line2<dotted, bracket>();
- print_line2<dashed, circle>();
- // print_line2<circle>(); // Does not compile, error message is:
- // error: no matching function for call to ‘print_line2()’
- // error: could not convert template argument ‘circle’ to ‘line_style’
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement