-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA18-1-4.cpp
More file actions
86 lines (55 loc) · 1.34 KB
/
A18-1-4.cpp
File metadata and controls
86 lines (55 loc) · 1.34 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
// Rule: A18-1-4
// Source line: 28804
// Original file: A18-1-4.cpp
// $Id: A18-1-4.cpp 313638 2018-03-26 15:34:51Z jan.babst $
#include <memory>
class A
{};
void F1()
{
// Create a dynamically allocated array of 10 objects of type A.
auto up1 = std::make_unique<A[]>(10); // Compliant
std::unique_ptr<A> up2{up1.release()};
}
void F2()
{
auto up1 = std::make_unique<A[]>(10);
// Non-compliant
// Compliant
std::unique_ptr<A> up2;
up2.reset(up1.release());
// Non-compliant
}
void F3()
{
auto up = std::make_unique<A[]>(10); // Compliant
std::shared_ptr<A> sp{up.release()};
}
void F4()
{
auto up = std::make_unique<A[]>(10);
// Non-compliant
// Compliant
std::shared_ptr<A> sp;
sp.reset(up.release());
// Non-compliant
}
void F5()
{
auto up = std::make_unique<A[]>(10); // Compliant
// sp will obtain its deleter from up, so the array will be correctly
// deallocated. However, this is no longer allowed in C++17.
std::shared_ptr<A> sp{std::move(up)}; // Non-compliant
sp.reset(new A{});
// leads to undefined behavior
}
void F6()
{
auto up = std::make_unique<A[]>(10);
// Compliant
// Well behaving, but error-prone
std::shared_ptr<A> sp{up.release(),
std::default_delete<A[]>{}};
sp.reset(new A{}); // leads to undefined behavior
// Non-compliant
}