-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplates.cpp
More file actions
169 lines (155 loc) · 3.16 KB
/
templates.cpp
File metadata and controls
169 lines (155 loc) · 3.16 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include <iostream>
#include <string.h>
// Old Code
// int Max(int x, int y) {
// return x > y ? x : y;
// }
// float Max(float x, float y) {
// return x > y ? x : y;
// }
// Primary Template
template <typename T>
T Max(T x, T y)
{
std::cout << typeid(T).name() << std::endl;
return x > y ? x : y;
}
// Explicit Instantiation
template char Max(char x, char y);
// Explicit Specialization
template <>
const char *Max<const char *>(const char *x, const char *y)
{
std::cout << "Max<const char*>()" << std::endl;
return strcmp(x, y) > 0 ? x : y;
}
void Examples()
{
#pragma region Instantiation Examples
Max(static_cast<float>(3), 5.5f);
// Override compiler's deduction process
Max<double>(3, 6.2);
// Causes implicit instantiation of Max<int>
int (*pfn)(int, int) = Max;
#pragma endregion
#pragma region Explicit Instantiation
const char *b{"B"};
const char *a{"A"};
auto s = Max(a, b);
std::cout << s << std::endl;
#pragma endregion
}
// Non-type template parameter
template <int size>
void Print()
{
char buffer[size];
std::cout << size << std::endl;
}
// Requires size as explicit argument
template <typename T>
T Sum(T *parr, int size)
{
T sum{};
for (int i = 0; i < size; ++i)
{
sum += parr[i];
}
return sum;
}
// Size is implicitly calculated in non-type template argument
template <typename T, int size>
T Sum(T (&arr)[size])
{
T sum{};
for (int i = 0; i < size; ++i)
{
sum += arr[i];
}
return sum;
}
int main_functions()
{
Examples();
Print<3>();
int arr[]{3, 1, 9, 7};
// int (&ref)[5] = arr ;
int sum = Sum(arr);
std::cout << sum << std::endl;
return 0;
}
// template classes
template <typename T, int size>
class Stack
{
T m_Buffer[size];
int m_Top{-1};
public:
Stack() = default;
Stack(const Stack<T, size> &obj)
{
m_Top = obj.m_Top;
for (int i = 0; i <= m_Top; ++i)
{
m_Buffer[i] = obj.m_Buffer[i];
}
}
void Push(const T &elem)
{
m_Buffer[++m_Top] = elem;
}
void Pop();
const T &Top() const
{
return m_Buffer[m_Top];
}
bool IsEmpty()
{
return m_Top == -1;
}
/*
Shorthand notation for class name as return type,
because it appears inside the class
*/
static Stack Create();
};
template <typename T, int size>
void Stack<T, size>::Pop()
{
--m_Top;
}
template <typename T, int size>
/*
Longhand notation for class name as return type,
because it appears outside the class
*/
Stack<T, size> Stack<T, size>::Create()
{
return Stack<T, size>();
}
int main()
{
main_functions();
/*
The template parameter list is part of the type of class.
The following code will not work.
Stack<float, 9> s = Stack<float, 10>::Create();
^
*/
Stack<const char *, 5> ss2;
ss2.Push("Hello");
Stack<float, 10> s = Stack<float, 10>::Create();
s.Push(3);
s.Push(1);
s.Push(6);
s.Push(9);
auto s2(s);
while (!s2.IsEmpty())
{
std::cout << s2.Top() << " ";
s2.Pop();
}
Stack<char *, 5> ss;
ss.Push("Hello");
return 0;
}