-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.cpp
More file actions
82 lines (65 loc) · 1.86 KB
/
exception.cpp
File metadata and controls
82 lines (65 loc) · 1.86 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
#include <iostream>
#include <exception>
using namespace std;
#if 0
class derivedexception: public exception {
virtual const char* what() const throw() {
return "My derived exception";
}
} myderivedexception;
int main () {
try {
throw myderivedexception;
}
catch (exception& e) {
cout << e.what() << '\n';
}
}
#endif
#include <iostream>
#include <typeinfo>
using namespace std;
void printTypeName(int x) {
cout << "printTypeName parameter type int " << typeid(x).name() << endl;
}
void printTypeName(float x) {
cout << "printTypeName parameter type float " << typeid(x).name() << endl;
}
void printTypeName(bool x) {
cout << "printTypeName parameter type bool " << typeid(x).name() << endl;
}
void printTypeName(double x) {
cout << "printTypeName parameter type double " << typeid(x).name() << endl;
}
template<typename T>
void printTemplateTypeName(T x) {
cout << "printTemplateTypeName typename parameter " << typeid(x).name() << endl;
}
int main() {
int myint = 5;
float myfloat = 7.987654321;
bool mybool = false;
double mydouble = 99.9;
//Overloaded printTypeName functions.
printTypeName(myint);
printTypeName(myfloat);
printTypeName(mybool);
printTypeName(mydouble);
//Temmplate function calls.
//Specify template function.
printTemplateTypeName<>(myint);
printTemplateTypeName<>(myfloat);
printTemplateTypeName<>(mybool);
printTemplateTypeName<>(mydouble);
//Implicit type parametrizing.
printTemplateTypeName(myint);
printTemplateTypeName(myfloat);
printTemplateTypeName(mybool);
printTemplateTypeName(mydouble);
//Explicit type parametrizing.
printTemplateTypeName<int>(myint);
printTemplateTypeName<float>(myfloat);
printTemplateTypeName<bool>(mybool);
printTemplateTypeName<double>(mydouble);
return 0;
}