-
Notifications
You must be signed in to change notification settings - Fork 0
/
weak-shared-proposal.html
141 lines (111 loc) · 5.38 KB
/
weak-shared-proposal.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
141
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=us-ascii">
<title>Proposal to Add Creation Functions Between Shared- and
Weak-Pointers</title>
<meta name="generator" content="Amaya, see http://www.w3.org/Amaya/">
<style type="text/css">
ins { background-color: #A0FFA0 }
del { background-color: #FFA0A0 }
</style>
</head>
<body>
<pre>Document number: Dnnnn
Project: Programming Language C++, Library Evolution Group
Date: 2014-01-23
Reply-to: Daryle Walker <darylew at gmail dot com></pre>
<h1>Proposal to Add Creation Functions Between Shared- and Weak-Pointers</h1>
<h2 id="Ch01">I. Table of Contents</h2>
<ol>
<li><a href="#Ch01">Table of Contents</a></li>
<li><a href="#Ch02">Introduction</a></li>
<li><a href="#Ch03">Motivation and Scope</a></li>
<li><a href="#Ch04">Impact on the Standard</a></li>
<li><a href="#Ch05">Design Decisions</a></li>
<li><a href="#Ch06">Technical Specifications</a></li>
<li><a href="#Ch07">Acknowledgments</a></li>
<li><a href="#Ch08">History</a></li>
<li><a href="#Ch09">References</a></li>
</ol>
<h2 id="Ch02">II. Introduction</h2>
<p>This proposal aims to add two function templates, to translate between
<code>std::shared_ptr</code> and <code>std::weak_ptr</code> instantiations.</p>
<h2 id="Ch03">III. Motivation and Scope</h2>
<p>When <code>std::shared_ptr</code> was added to C++-2011, the creation
function <code>std::make_shared</code> was also added. Not only does that
function facilitate an "always use <code>auto</code>" approach with smart
pointers, it introduced an efficiency in that a <code>shared_ptr</code>'s data
and control blocks are allocated together. This model was appreciated enough
that is planned to be extended for C++-2014 by <code>std::make_unique</code>
for the alternate smart-pointer <code>std::unique_ptr</code>.</p>
<p>The blog post "Automatically Made Weak" by Brett Hall postulated a
smart-pointer creation for another Standard smart pointer,
<code>std::weak_ptr</code>, from a <code>std::shared_ptr</code>. That blog post
was flagged by another at the Standard C++ announcement site, and the first
comment at the latter blog postulated a conversion function in the other
direction.</p>
<h2 id="Ch04">IV. Impact on the Standard</h2>
<p>The proposal adds two function templates that forward to some constructors.
The implementation should work with current language features.</p>
<h2 id="Ch05">V. Design Decisions</h2>
<h2 id="Ch06">VI. Technical Specifications</h2>
<p>The changes are based off C++ Working Draft Standard N3797. New sections and
paragraphs have letters as label place-holders; the final numbers, plus moving
existing label numbers to fit, are to be determined later.</p>
<p>In section 20.8.2.2 [util.smartptr.shared], add to the part of the synopsis
about creation:</p>
<blockquote>
<pre><i>// 20.8.2.2.6, shared_ptr creation</i>
template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args);
template<class T, class A, class... Args>
shared_ptr<T> allocate_shared(const A& a, Args&&... args);
<ins>template<class T, class Y>
shared_ptr<T> try_shared(const weak_ptr<Y>& r);</ins></pre>
</blockquote>
<p>Modify section 20.8.2.2.6 [util.smartptr.shared.create] by appending another
function:</p>
<blockquote>
<pre>template<class T, class Y> shared_ptr<T> try_shared(const weak_ptr<Y>& r);</pre>
<p><strong>-X-</strong> <em>Requires:</em>
<code>shared_ptr<T>{r}</code> would be well formed.</p>
<p><strong>-X + 1-</strong> <em>Returns:</em> <code>shared_ptr<T>{ r
}</code>.</p>
<p><strong>-X + 2-</strong> <em>Throws:</em> <code>bad_weak_ptr</code> when
<code>r.expired()</code>.</p>
</blockquote>
<p>In section 20.8.2.3 [util.smartptr.weak], add to the synopsis before the
specialized algorithms:</p>
<blockquote>
<pre><ins><i>// 20.8.2.3.A, weak_ptr creation</i>
template<class T, class Y>
weak_ptr<T> make_weak(shared_ptr<Y> const& r) noexcept;</ins>
<i>// 20.8.2.3.6, specialized algorithms</i></pre>
</blockquote>
<p>Add new section 20.8.2.3.A [util.smartptr.weak.create] before 20.8.2.3.6
[util.smartptr.weak.spec], titled "<strong><code>weak_ptr</code>
creation</strong>":</p>
<blockquote>
<pre>template<class T, class Y> weak_ptr<T> make_weak(shared_ptr<Y> const& r) noexcept;</pre>
<p><strong>-1-</strong> <em>Requires:</em> <code>weak_ptr<T>{r}</code>
would be well formed.</p>
<p><strong>-2-</strong> <em>Returns:</em> <code>weak_ptr<T>{ r
}</code>.</p>
</blockquote>
<h2 id="Ch07">VII. Acknowledgments</h2>
<h2 id="Ch08">VIII. History</h2>
<h2 id="Ch09">IX. References</h2>
<ul>
<li>"Automatically Made Weak," Brett Hall, 2013 Nov 12. <a
href="http://backwardsincompatibilities.wordpress.com/2013/11/12/automatically-made-weak/">backwardsincompatibilities.wordpress.com/2013/11/12/automatically-made-weak/</a></li>
<li>"Automatically Made Weak—Brett Hall"; News, Status & Discussion
about Standard C++; Nov 12, 2013 02:13 PM. <a
href="http://isocpp.org/blog/2013/11/automatically-made-weak-brett-hall">isocpp.org/blog/2013/11/automatically-made-weak-brett-hall</a>
<ul>
<li>comment; NoSenseEtAl; Nov 15, 2013 05:41 PM.</li>
</ul>
</li>
</ul>
</body>
</html>