-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolicy_based_programming.cpp
More file actions
44 lines (34 loc) · 1.05 KB
/
policy_based_programming.cpp
File metadata and controls
44 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
// a general class
// template type and its also base class
// we try to access base class members inside of worker
// policy will response our questions like necessary information
// metaprogramming is nice here because it allows compile time type generation so dynamic
template <class T>
struct Printer: private T {
void print() {
auto width = T::get_width();
auto color = T::get_color();
auto font = T::get_font();
(void)width;
(void)color;
(void)font;
std::cout << "printing" << std::endl;
}
};
struct Arial{
constexpr const char* get_font() const { return "arial"; }
constexpr const char* get_color() const { return "red"; }
constexpr int get_width() const { return 10; }
};
struct Times {
constexpr const char* get_font() const { return "times"; }
constexpr const char* get_color() const { return "black"; }
constexpr int get_width() const { return 11; }
};
int main() {
Printer<Arial> p;
Printer<Times> t;
p.print();
t.print();
}