-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA15-5-1.cpp
More file actions
202 lines (137 loc) · 3.16 KB
/
A15-5-1.cpp
File metadata and controls
202 lines (137 loc) · 3.16 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Rule: A15-5-1
// Source line: 26541
// Original file: A15-5-1.cpp
//% $Id: A15-5-1.cpp 309720 2018-03-01 14:05:17Z jan.babst $
#include <stdexcept>
#include <type_traits>
class C1
{
public:
C1() = default;
// Compliant - move constructor is non-throwing and declared to be noexcept
C1(C1&& rhs) noexcept {}
// Compliant - move assignment operator is non-throwing and declared to be
// noexcept
C1& operator=(C1&& rhs) noexcept {
return *this;
}
// Compliant - destructor is non-throwing and declared to be noexcept by
// default
~C1() noexcept {}
};
void Swap(C1& lhs,
C1& rhs) noexcept
{
// Implementation
// Compliant - swap function is non-throwing and
// declared to be noexcept
}
class C2
{
public:
C2() = default;
// Compliant - move constructor is non-throwing and declared to be noexcept
C2(C2&& rhs) noexcept
{
try
{
// ...
throw std::runtime_error(
"Error"); // Exception will not escape this function
}
catch (std::exception& e)
{
// Handle error
}
}
C2& operator=(C2&& rhs) noexcept
{
try
{
// ...
throw std::runtime_error(
"Error"); // Exception will not escape this function
}
catch (std::exception& e)
{
// Handle error
}
return *this;
}
// Compliant - destructor is non-throwing and declared to be noexcept by
// default
~C2()
{
try
{
// ...
throw std::runtime_error(
"Error"); // Exception will not escape this function
}
catch (std::exception& e)
{
// Handle error
}
}
};
// Non-compliant - swap function is declared to be noexcept(false)
void Swap(C2& lhs, C2& rhs) noexcept(false)
{
// ...
// Non-compliant - Implementation exits with an exception
throw std::runtime_error("Swap function failed");
}
class C3
{
public:
C3() = default;
C3(C3&& rhs) // Non-compliant - move constructor throws
{
// ...
throw std::runtime_error("Error");
}
C3& operator=(C3&& rhs) // Non-compliant - move assignment operator throws
{
// ...
throw std::runtime_error("Error");
return *this;
}
~C3() // Non-compliant - destructor exits with an exception
{
throw std::runtime_error("Error");
}
static void operator delete(void* ptr, std::size_t sz)
{
// ...
throw std::runtime_error("Error"); // Non-compliant - deallocation
// function exits with an exception
}
};
void Fn()
{
C3 c1; // program terminates when c1 is destroyed
C3* c2 = new C3;
// ...
delete c2; // program terminates when c2 is deleted
}
template<typename T>
class Optional
{
public:
// ...
// Compliant by exception
Optional(Optional&& other) noexcept(
std::is_nothrow_move_constructible<T>::value)
{
// ...
}
// Compliant by exception
Optional& operator=(Optional&& other) noexcept(
std::is_nothrow_move_assignable<T>::value &&
std::is_nothrow_move_constructible<T>::value)
{
// ...
return *this;
}
// ...
};