-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMy_Point.hpp
More file actions
155 lines (133 loc) · 3.69 KB
/
My_Point.hpp
File metadata and controls
155 lines (133 loc) · 3.69 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
#pragma once
#include <concepts>
#include <type_traits>
template<typename T>
struct My_Point
{
public:
T x;
T y;
public:
//构造、析构、拷贝构造、移动构造、拷贝赋值、移动赋值、
//全部让其自动生成
#define OPERATOR_XSELF_OBJ(op)\
My_Point &operator op(const My_Point &_Right) noexcept\
requires requires(T a, T b) {{ a op b } -> std::convertible_to<T>;}\
{\
x op _Right.x;\
y op _Right.y;\
return *this;\
}
#define OPERATOR_XSELF_NUM(op)\
My_Point &operator op(const T & tNum) noexcept\
requires requires(T a, T b) {{ a op b } -> std::convertible_to<T>;}\
{\
x op tNum;\
y op tNum;\
return *this;\
}
#define OPERATOR_XCOPY_OBJ(op)\
My_Point operator op(const My_Point &_Right) const noexcept\
requires requires(T a, T b) {{ a op b } -> std::convertible_to<T>;}\
{\
return My_Point\
{\
x op _Right.x,\
y op _Right.y,\
};\
}
#define OPERATOR_XCOPY_NUM(op)\
My_Point operator op(const T & tNum) const noexcept\
requires requires(T a, T b) {{ a op b } -> std::convertible_to<T>;}\
{\
return My_Point\
{\
x op tNum,\
y op tNum,\
};\
}
#define OPERATOR_GENERATOR(op)\
OPERATOR_XSELF_OBJ(op##=)\
OPERATOR_XSELF_NUM(op##=)\
OPERATOR_XCOPY_OBJ(op)\
OPERATOR_XCOPY_NUM(op)
//批量生成
OPERATOR_GENERATOR(+);
OPERATOR_GENERATOR(-);
OPERATOR_GENERATOR(*);
OPERATOR_GENERATOR(/);
OPERATOR_GENERATOR(%);
OPERATOR_GENERATOR(<<);
OPERATOR_GENERATOR(>>);
#undef OPERATOR_GENERATOR
#undef OPERATOR_XCOPY_NUM
#undef OPERATOR_XCOPY_OBJ
#undef OPERATOR_XSELF_NUM
#undef OPERATOR_XSELF_OBJ
//布尔运算
//注意必须std::convertible_to<bool>,并不多余,因为存在自定义重载返回非bool情况
bool operator==(const My_Point &_Right)
requires requires(T a, T b) {{ a == b } -> std::convertible_to<bool>;}
{
return x == _Right.x && y == _Right.y;
}
bool operator==(const T &tNum)
requires requires(T a, T b) {{ a == b } -> std::convertible_to<bool>;}
{
return x == tNum && y == tNum;
}
bool operator!=(const My_Point &_Right)
requires requires(T a, T b) {{ a == b } -> std::convertible_to<bool>;}
{
return x != _Right.x || y != _Right.y;
}
bool operator!=(const T &tNum)
requires requires(T a, T b) {{ a == b } -> std::convertible_to<bool>;}
{
return x != tNum || y != tNum;
}
//不重载大于、小于判断,因为无法知道如何确认大小
};
//修改testType查看是否正确控制运算符生成
#define TEST
#ifdef TEST
void ______Test______(void)
{
using testType = int;
My_Point<testType> testPoint{};
My_Point<testType> testPoint2{};
My_Point<testType> testPoint3{};
testPoint == testType{};
testPoint != testType{};
testPoint == testPoint2;
testPoint != testPoint2;
testPoint += testType{};
testPoint -= testType{};
testPoint *= testType{};
testPoint /= testType{};
testPoint %= testType{};
testPoint <<= testType{};
testPoint >>= testType{};
testPoint3 = testPoint + testType{};
testPoint3 = testPoint - testType{};
testPoint3 = testPoint * testType{};
testPoint3 = testPoint / testType{};
testPoint3 = testPoint % testType{};
testPoint3 = testPoint << testType{};
testPoint3 = testPoint >> testType{};
testPoint += testPoint2;
testPoint -= testPoint2;
testPoint *= testPoint2;
testPoint /= testPoint2;
testPoint %= testPoint2;
testPoint <<= testPoint2;
testPoint >>= testPoint2;
testPoint3 = testPoint + testPoint2;
testPoint3 = testPoint - testPoint2;
testPoint3 = testPoint * testPoint2;
testPoint3 = testPoint / testPoint2;
testPoint3 = testPoint % testPoint2;
testPoint3 = testPoint << testPoint2;
testPoint3 = testPoint >> testPoint2;
}
#endif