-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD1069R1_bad_merge.html
140 lines (133 loc) · 6.81 KB
/
D1069R1_bad_merge.html
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
<!DOCTYPE html>
<html lang="en"><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style>
.comment { color: #999999; font-style: italic; }
.pre { color: #000099; }
.string { color: #009900; }
.char { color: #009900; }
.float { color: #996600; }
.int { color: #999900; }
.bool { color: #000000; font-weight: bold; }
.type { color: #FF6633; }
.flow { color: #FF0000; }
.keyword { color: #990000; }
.operator { color: #663300; font-weight: bold; }
.operator { color: #663300; font-weight: bold; }
pre.code {
border: 2px solid #666;
background-color: #F4F4F4;
padding-left: 10px;
padding-top: 0px;
}
code {
border: 2px solid #d0d0d0;
background-color: LightYellow;
padding: 2px;
padding-left: 10px;
display:table;
white-space:pre;
margin:2px;
margin-bottom:10px;
}
dt {
font-weight: bold;
}
.ins {
background-color:#A0FFA0;
}
.del {
background-color:#FFA0A0;
text-decoration:line-through
}
.TODO {
background-color: LightYellow;
color: red;
}
</style>
<title>Inferencing heap objects</title>
</head>
<body>
<p>Document number: D1069R1 <br>
Date: 2018-11-25<br>
Reply-To:<br>
Mike Spertus, Symantec (<a href="mailto:mike_spertus@symantec.com">mike_spertus@symantec.com</a>)<br>
Walter E. Brown (<a href="mailto:webrown.cpp@gmail.com"> webrown.cpp@gmail.com</a>)<br>
Stephan T. Lavavej (<a href="mailto:stl@exchange.microsoft.com">stl@exchange.microsoft.com</a>)<br>
Audience: {Library Evolution, Library} Working Group
</p>
<h2>Changes from previous version</h2>
Now focuses entirely on inferencing heap objects. The remaining portions of P1069R0 may be considered later in separate papers.
<h1>Inferencing heap objects</h1>
While class template argument deduction can be used for
objects with static and automatic duration, it generally cannot be
used for creating objects with dynamic duration as the following attempts show:
<code>optional o(5); // OK. Static duration. optional<int>
// Try to create dynamic duration object.
auto o1 = new optional(5); // Already violates both Core Guidelines <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-ptr">R.3</a> and <a href="https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-newdelete">R.11</a><br>
<strike>auto o2 = unique_ptr(o1);</strike> // Oops, ill-formed. Can't deduce unique_ptr from raw ptr</code>
We propose simply making dynamic object creation with <tt>make_unique</tt>, <tt>make_shared</tt>,
and <tt>allocate_shared</tt>
work just like the normal way one would translate a static and automatic declaration to a dynamic
declaration
<code>int i1(5); // Static
auto i2 = make_unique<int>(5); // Make dynamic by moving decl-specifier to make_unique template argument
option o1(5);
auto o2 = make_unique<optional>(5); // Again, make dynamic by moving decl-specifier to make_unique template argument</code>
Implementing this can be as simple as the following code, which can be seen working on <a href="https://wandbox.org/permlink/jn49SA7tlDO02gcw">Wandbox</a>
<code>template<template <typename ...U> typename T, typename ...A>
auto make_unique(A&& ...a) {
return std::unique_ptr<decltype(T(std::forward<A>(a)...))>(new T(std::forward<A>(a)...));
}</code>
<b>Notes:</b>
<ul><li>We only provide overloads for creating non-array objects as the other examples are not natural to deduce</li>
<li>For simplicity, we do not propose supporting the case of non-type template parameters as
it is far less frequent and seems far more complex.
If and only if library implementers feel that there are no implementation concerns for non-type
template parameters, then that should be supported as well</li>
<li>It is worth pointing out that because this code exactly matches <em>mutatis mutandi</em> the automatic duration
declaration case, it should have no cause to be in any way more dangerous or confusing
that what is supported for objects created with static or automatic duration</li></ul>
<h2>Wording</h2>
<blockquote><pre>//23.11.1, class template unique_ptr
template<class T> struct default_delete;
template<class T> struct default_delete<T[]>;
template<class T, class D = default_delete<T>> class unique_ptr;
template<class T, class D> class unique_ptr<T[], D>;
template<class T, class... Args> unique_ptr<T>
make_unique(Args&&... args); // T is not array
<ins>template<template <class ...U> class T, class ...Args>
auto make_unique(Args&&... args) -> unique_ptr<decltype(T(std::forward<Args>(args)...))>;</ins>
template<class T> unique_ptr<T>
make_unique(size_t n); // T is U[]</pre>
<pre>//23.11.3.6, shared_ptr creation
template<class T, class... Args>
shared_ptr<T> make_shared(Args&&... args); // T is not array
<ins>template<template <class ...U> class T, class ...Args>
auto make_shared(Args&&... args) -> shared_ptr<decltype(T(std::forward<Args>(args)...))>;</ins>
template<class T, class A, class... Args>
shared_ptr<T> allocate_shared(const A& a, Args&&... args); // T is not array
<ins>template<template <class ...U> class T, class A, class ...Args>
auto allocate_shared(const A&, Args&&... args) -> shared_ptr<decltype(T(std::forward<Args>(args)...))>;</ins>
</pre>
<pre>23.11.1.4 unique_ptr creation [unique.ptr.create]
template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args);
<em>Remarks</em>: This function shall not participate in overload resolution unless T is not an array.2
<em>Returns</em>: unique_ptr<T>(new T(std::forward&Args>(args)...)).
<ins>template<template <class ...U> class T, class ...Args>
auto make_unique(Args&&... args) -> unique_ptr<decltype(T(std::forward<Args>(args)...))>;
<em>Returns</em>: unique_ptr<decltype(T(std::forward<A>(a)...))>(new T(std::forward<A>(a)...))</ins>
</pre></blockquote>
<blockquote>23.11.3.6 shared_ptr creation [util.smartptr.shared.create]<br>
The common requirements that apply to all make_shared and allocate_shared overloads, unless specified
otherwise, are described below.
<pre>template<class T, ...>
shared_ptr<T> make_shared(args);
<ins>template<template <class ...U> class T, class ...Args>
auto make_shared(Args&&... args) -> shared_ptr<decltype(T(std::forward<Args>(args)...))>;</ins>
template<class T, class A, ...>
shared_ptr<T> allocate_shared(const A& a, args);
<ins>template<template <class ...U> class T, class A, class ...Args>
auto allocate_shared(const A&, Args&&... args) -> shared_ptr<decltype(T(std::forward<Args>(args)...))>;</ins>
</pre></blockquote>
</body></html>