-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA12-8-2.cpp
More file actions
87 lines (67 loc) · 1.39 KB
/
A12-8-2.cpp
File metadata and controls
87 lines (67 loc) · 1.39 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
// Rule: A12-8-2
// Source line: 18360
// Original file: A12-8-2.cpp
// $Id: A12-8-2.cpp 289436 2017-10-04 10:45:23Z michal.szczepankiewicz $
#include <cstdint>
#include <utility>
class A
{
public:
A(const A& oth)
{
// ...
}
A(A&& oth) noexcept
{
// ...
}
A& operator=(const A& oth) & // Compliant
{
A tmp(oth);
Swap(*this, tmp);
return *this;
}
A& operator=(A&& oth) & noexcept // Compliant
{
A tmp(std::move(oth));
Swap(*this, tmp);
return *this;
}
static void Swap(A& lhs, A& rhs) noexcept
{
std::swap(lhs.ptr1, rhs.ptr1);
std::swap(lhs.ptr2, rhs.ptr2);
}
private:
std::int32_t* ptr1;
std::int32_t* ptr2;
};
class B
{
public:
B& operator=(const B& oth) & // Non-compliant
{
if (this != &oth)
{
ptr1 = new std::int32_t(*oth.ptr1);
ptr2 = new std::int32_t(
*oth.ptr2); // Exception thrown here results in
// a memory leak of ptr1
}
return *this;
}
B& operator=(B&& oth) & noexcept // Non-compliant
{
if (this != &oth)
{
ptr1 = std::move(oth.ptr1);
ptr2 = std::move(oth.ptr2);
oth.ptr1 = nullptr;
oth.ptr2 = nullptr;
}
return *this;
}
private:
std::int32_t* ptr1;
std::int32_t* ptr2;
};