-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCommonType.cpp
More file actions
98 lines (77 loc) · 2.84 KB
/
CommonType.cpp
File metadata and controls
98 lines (77 loc) · 2.84 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
// =====================================================================================
// CommonType.cpp // std::common_type
// =====================================================================================
module modern_cpp:common_type;
namespace CommonType {
template <typename T>
class Number {
private:
T m_value;
public:
Number() : Number{ T{} } {}
Number(T value) : m_value{ value } {}
T operator()() const {
return m_value;
}
};
template <typename T, typename U>
Number<typename std::common_type<T, U>::type> operator+ (
const Number<T>& lhs,
const Number<U>& rhs)
{
using result_type = typename std::common_type<T, U>::type;
result_type result;
result = lhs() + rhs();
return result;
}
// or with trailing return type notation:
//template <typename T, typename U>
//auto operator+ (
// const Number<T>& lhs,
// const Number<U>& rhs) -> Number<typename std::common_type<T, U>::type>
//{
// using result_type = typename std::common_type<T, U>::type;
// result_type result;
// result = lhs() + rhs();
// return result;
//}
static void test_commontype_01()
{
std::cout << "std::common_type demonstration:" << std::endl;
Number<int> intNumber(123);
std::cout << intNumber() << std::endl;
Number<long> longNumber(321);
std::cout << longNumber() << std::endl;
Number<std::common_type<int, long>::type> result;
result = intNumber() + longNumber();
std::cout << result() << std::endl;
Number<short> shortNumber(321);
std::cout << shortNumber() << std::endl;
Number<double> doubleNumber(123.456);
std::cout << doubleNumber() << std::endl;
Number<std::common_type<short, double>::type> anotherResult = shortNumber() + doubleNumber();
std::cout << anotherResult() << std::endl;
}
// do you notice the difference using operator+ between these two examples?
// (hint: the debugger may help ...)
static void test_commontype_02()
{
std::cout << "More std::common_type demonstration:" << std::endl;
Number<double> doubleNumber(123.456);
std::cout << doubleNumber() << std::endl;
Number<long> longNumber(321);
std::cout << longNumber() << std::endl;
Number<std::common_type<double, long>::type> result;
result = doubleNumber + longNumber;
std::cout << result() << std::endl;
}
}
void main_common_type()
{
using namespace CommonType;
test_commontype_01();
test_commontype_02();
}
// =====================================================================================
// End-of-File
// =====================================================================================