-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
92 lines (85 loc) · 2.08 KB
/
Main.cpp
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
#include "Sum.hpp"
#include <iostream>
#include <cxxabi.h>
#include <assert.h>
using namespace std;
using namespace cs540;
ostream &
operator<<(ostream &os, const type_info &ti) {
int ec;
const char *demangled_name = abi::__cxa_demangle(ti.name(), 0, 0, &ec);
assert(ec == 0);
os << demangled_name;
free((void *) demangled_name);
return os;
}
struct B;
struct C;
struct D;
struct E;
struct A {
friend A operator+(const A &, const B &);
friend A operator+(const A &, const C &);
friend A operator+(const A &, const D &);
friend A operator+(const A &, const E &);
friend ostream & operator<<(ostream &os, const A &a);
A(int i_ = 0) : i(i_) {}
private:
const int i;
};
struct B {
friend A operator+(const A &a, const B &b) {
return A(a.i + b.i);
}
B(int i_ = 0) : i(i_) {}
private:
const int i;
};
struct C {
friend A operator+(const A &a, const C &c) {
return A(a.i + c.i);
}
C(int i_ = 0) : i(i_) {}
private:
const int i;
};
struct D {
friend A operator+(const A &a, const D &d) {
return A(a.i + d.i);
}
D(int i_ = 0) : i(i_) {}
private:
const int i;
};
struct E {
friend A operator+(const A &a, const E &e) {
return A(a.i + e.i);
}
E(int i_ = 0) : i(i_) {}
private:
const int i;
};
ostream &
operator<<(ostream &os, const A &a) {
return os << a.i;
}
/*
ostream &
operator<<(ostream &os, const B &) {
return os << "B object";
}
*/
int main() {
// sum(A(), B(), C());
//cout << typeid(sum(A(), B(), C())) << endl;
cout << sum(A(1), B(2), C(3)) << endl;
cout << sum(A(1), B(2), B(3)) << endl;
cout << sum(A(1), C(2), C(3)) << endl;
cout << sum(A(1), C(2)) << endl;
cout << hex << showbase;
// Prints 1, due to overflow.
cout << sum(0xffffffffU, 1U, 1UL) << endl;
// Prints 0x100000001, due to first promoting to UL.
cout << sum(1UL, 1U, 0xffffffffU) << endl;
//cout << sum(1, 3.1, 2) << endl;
}