-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello.cpp
More file actions
53 lines (44 loc) · 1.13 KB
/
Copy pathhello.cpp
File metadata and controls
53 lines (44 loc) · 1.13 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
#include <iostream>
using namespace std;
template<typename T> void myswap(T& t1, T& t2); // template
template<> void myswap(char& t1, char& t2); // char specialization
int func(int n);
template void myswap(int& t1, int& t2); // implicit instantiate, non <>
int main() {
long a = 71, b = 73;
myswap<long>(a, b);
cout << "a, b = " << a << ", " << b << endl;
// int a = 'A', b = 'B';
// myswap(a, b);
// myswap('a', 'b'); // error
// cout << "a, b = " << a << ", " << b << endl;
// pointer of an array of function pointer
// int (*func_arr[3])(int) = {func, func, func};
// int (*(*func_arr_p)[3])(int) = &func_arr;
// (*func_arr_p)[1](2);
// array of function pointer
// int (*func_arr[3])(int) = {func, func, func};
// func_arr[1](4);
// function pointer
// int (*pf)(int) = func;
// (*pf)(4);
// pf(3);
}
template<typename T> void myswap(T& t1, T& t2) {
cout << "template of T" << endl;
T temp = t1;
t1 = t2;
t2 = temp;
}
template<> void myswap(char& t1, char& t2) {
cout << "char specialization" << endl;
char temp = t1;
t1 = t2;
t2 = temp;
}
int func(int n){
while(n > 0) {
cout << "function!" << endl;
n--;
}
}