-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLWG2228.html
619 lines (538 loc) · 20 KB
/
LWG2228.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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>LWG 2228: Missing SFINAE rule in unique_ptr templated assignment</title>
<style type="text/css">
p {text-align:justify;}
li {text-align:justify;}
blockquote.note
{
background-color:#E0E0E0;
padding-left: 15px;
padding-right: 15px;
padding-top: 1px;
padding-bottom: 1px;
}
ins {background-color:#A0FFA0;}
del {background-color:#FFA0A0;}
address {text-align:right;}
h1 {text-align:center;}
span.comment {color:#C80000;}
table {border-width:1px; border-color:black; border-style:solid;}
td {border-width:1px; border-color:black; border-style:solid; padding:5px;}
</style>
</head>
<body>
<address>
Document number: D4366<br/>
<br/>
<a href="mailto:howard.hinnant@gmail.com">Howard Hinnant</a><br>
2015-01-11<br/>
</address>
<hr>
<h1>LWG 2228: Missing <i>SFINAE</i> rule in <code>unique_ptr</code> templated assignment</h1>
<h2>Contents</h2>
<ul>
<li><a href="#Introduction">Introduction</a></li>
<li><a href="#CurrentFix">The Current Proposed Fix</a></li>
<li><a href="#ProblemCurrentFix">The Problem with the Current Proposed Fix</a></li>
<li>
<a href="#CorrectFix">Wording</a>
<ul>
<li><a href="#CorrectFix">So What Is The Correct Fix?</a></li>
<li><a href="#CorrectFixContinued">And <code>unique_ptr<T[]></code> Needs The Fix Too</a></li>
</ul></li>
<li><a href="#ComplexExample">Appendix 1: A More Complicated Example</a></li>
<li><a href="#Acknowledgments">Acknowledgments</a></li>
</ul>
<h2><a name="Introduction"></a>Introduction</h2>
<p>
This paper addresses the <a href="http://cplusplus.github.io/LWG/lwg-active.html#2228">Library Working Group Issue 2228</a>.
The crux of this issue is that the converting move assignment operator of
<code>unique_ptr</code> will participate in overload resolution, even if the
actual implementation of <code>unique_ptr</code> move assignment operator will
not compile.
</p>
<p>
For example:
</p>
<blockquote><pre>
#include <memory>
#include <type_traits>
struct do_nothing
{
template <class T>
void operator()(T*) {}
};
int
main()
{
int i = 0;
std::unique_ptr<int, do_nothing> p1(&i);
std::unique_ptr<int> p2;
<font color="#00A000">// This mistakenly compiles:</font>
static_assert(std::is_assignable<decltype(p2), decltype(p1)>::value, "");
<font color="#00A000">// But this correctly does not compile:</font>
p2 = std::move(p1);
}
</pre></blockquote>
<p>
The <code>static_assert</code> compiles because of the <i>Remarks:</i> clause
of the <code>unique_ptr</code> converting move assignment operator in
[unique.ptr.single.asgn]:
</p>
<blockquote>
<pre>
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
</pre>
<blockquote>
<p>
<i>Remarks:</i> This operator shall not participate in overload resolution unless:
</p>
<ul>
<li><code>unique_ptr<U, E>::pointer</code> is implicitly convertible to
<code>pointer</code> and</li>
<li><code>U</code> is not an array type.</li>
</ul>
</blockquote>
</blockquote>
<p>
In our example both <code>unique_ptr<U, E>::pointer</code> and <code>pointer</code>
have type <code>int*</code>, and so the first bullet is satisfied. And <code>U</code>
has type <code>int</code>, which is not an array type, and so the second bullet
is also satisfied. Thus this signature shall participate in overload resolution,
and subsequently, as far as <code>is_assignable</code> is concerned, the move
assignment in our example above looks well formed.
</p>
<p>
However the trouble comes when we look at the <i>Effects:</i> clause of this
operator:
</p>
<blockquote>
<pre>
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
</pre>
<blockquote>
<p>
<i>Effects:</i> Transfers ownership from <code>u</code> to <code>*this</code> as
if by calling <code>reset(u.release())</code> followed by
<code>get_deleter() = std::forward<E>(u.get_deleter())</code>.
</p>
</blockquote>
</blockquote>
<p>
The <i>Effects:</i> clause is correct, but the trouble is with the assignment
of the two deleter types. In our example <code>get_deleter()</code> is an
lvalue expression of type <code>std::default_delete<int></code>,
<code>E</code> has type <code>do_nothing</code>, and
<code>u.get_deleter()</code> is an lvalue expression of type
<code>do_nothing</code>.
</p>
<p>
<code>std::default_delete<int></code> can not be assigned to by a
<code>do_nothing</code>. And thus even though the standard trait
<code>is_assignable</code> says we can move assign <code>p1</code> to <code>p2</code>,
when we try to, we get an error from within the implementation of the std::lib.
With clang/libc++ the error looks like:
</p>
<blockquote><pre>
libcxx/include/memory:2563:33: <font color="#C80000">error:</font> no viable overloaded '='
__ptr_.second() = _VSTD::forward<_Ep>(__u.get_deleter());
<font color="#00A000">~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</font>
</pre></blockquote>
<p>
The complaint of <a href="http://cplusplus.github.io/LWG/lwg-active.html#2228">LWG 2228</a>
is right on the money: This is an intolerable situation. This is a defect in
the <code>unique_ptr</code> specification.
</p>
<h2><a name="CurrentFix"></a>The Current Proposed Fix</h2>
<p>
<a href="http://cplusplus.github.io/LWG/lwg-active.html#2228">LWG 2228</a>
currently proposes to fix this bug by adding the following additional constraint
to the <i>Remarks:</i> clause:
</p>
<blockquote>
<pre>
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
</pre>
<blockquote>
<p>
<i>Remarks:</i> This operator shall not participate in overload resolution unless:
</p>
<ul>
<li><code>unique_ptr<U, E>::pointer</code> is implicitly convertible to
<code>pointer</code> and</li>
<li><code>U</code> is not an array type<del>.</del><ins>, and</ins></li>
<li><ins>either <code>D</code> is a reference type and <code>E</code> is the
same type as <code>D</code>, or <code>D</code> is not a reference type and
<code>E</code> is implicitly convertible to <code>D</code>.</ins></li>
</ul>
</blockquote>
</blockquote>
<p>
And indeed, when we apply this extra constraint, this fixes our example
above! Neither <code>D</code> nor <code>E</code> are reference types.
However <code>E</code> (<code>do_nothing</code>) is not implicitly
convertible to <code>D</code> (<code>std::default_delete<int></code>),
and so our <code>static_assert</code> now correctly refuses to compile.
</p>
<p>
However there is a problem with this fix. The big picture problem is that
the constraint does not describe what the implementation actually needs to
do. It instead describes a constraint that is in fact applicable only to the
<code>unique_ptr</code> converting move constructor. It happens to work in
our first example above. However in <a href="#ComplexExample">Appendix 1</a>
a more complicated example is presented which the current proposed wording
breaks.
</p>
<h2><a name="ProblemCurrentFix"></a>The Problem with the Current Proposed Fix</h2>
<p>
The current proposed wording fails to compile the currently valid,
motivating, and non-contrived example in <a href="#ComplexExample">Appendix
1</a>. The error message is:
</p>
<blockquote><pre>
test.cpp:130:12: <font color="#C80000">error:</font> no viable overloaded '='
pb = std::move(pd);
<font color="#00A000">~~ ^ ~~~~~~~~~~~~~</font>
</pre></blockquote>
<p>
The reason it fails to compile this example is because of the constraint that
if both deleters are reference types, they must be the same type. But in this
example our two deleter types are:
</p>
<ul>
<li><code>deleter<base>&</code></li>
<li><code>deleter<derived>&</code></li>
</ul>
<p>
Even though the author of <code>deleter</code> went to the trouble to allow a
converting copy assignment from <code>deleter<derived></code> to
<code>deleter<base></code> (and not vice-versa), and even though this is
exactly what the specification of <code>unique_ptr</code> says it will do, the
current proposed wording breaks this valid, motivating, non-contrived, and
potentially already-existing-in-the-wild example.
</p>
<h2><a name="CorrectFix"></a>So What Is The Correct Fix?</h2>
<p>
To introduce the correct fix, it is helpful to go back to the "big picture"
problem mentioned much earlier in this paper:
</p>
<blockquote><p>
The big picture problem is that the constraint does not describe what the
implementation actually needs to do.
</p></blockquote>
<p>
What does the implementation actually need to do?
</p>
<blockquote><pre>
get_deleter() = std::forward<E>(u.get_deleter())
</pre></blockquote>
<p>
This is the current specification from the <i>Effects:</i> clause and there is
no dispute that this part of the specification is correct. And creating a
constraint for exactly this specification is easy to both implement and specify:
</p>
<p>
Change [unique.ptr.single.asgn]:
</p>
<blockquote>
<pre>
template <class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept;
</pre>
<blockquote>
<p>
<i>Remarks:</i> This operator shall not participate in overload resolution unless:
</p>
<ul>
<li><code>unique_ptr<U, E>::pointer</code> is implicitly convertible to
<code>pointer</code> and</li>
<li><code>U</code> is not an array type<del>.</del><ins>, and</ins></li>
<li><ins><code>is_assignable<D&, E&&>::value</code> is
true.</ins></li>
</ul>
</blockquote>
</blockquote>
<p>
This additional constraint correctly handles both the non-reference deleter case
and the reference deleter case. It constrains precisely what the <i>Effects:</i>
clause specifies — no more, and no less. This means that no existing
compiling code will cease compiling. The only impact the client will see is
that <code>is_assignable</code> will correctly model the <code>unique_ptr</code>
converting move assignment operator. That is, it will answer true in all
existing cases that compile, and it will answer false in cases that already do
not compile.
</p>
<h2><a name="CorrectFixContinued"></a>And <code>unique_ptr<T[]></code> Needs The Fix Too</h2>
<p>
<code>unique_ptr<T[]></code> has the same defect for the same reasons as
the primary template. The only difference is that an incorrect fix has already
been applied to the current working draft, and is corrected herein:
</p>
<p>
Change [unique.ptr.runtime.asgn]:
</p>
<blockquote>
<pre>
template <class U, class E>
unique_ptr& operator=(unique_ptr<U,E>&& u)noexcept;
</pre>
<blockquote>
<p>
This operator behaves the same as in the primary template, except that it
shall not participate in overload resolution unless all of the following
conditions hold, where <code>UP</code> is <code>unique_ptr<U, E></code>:
</p>
<ul>
<li>
<code>U</code> is an array type, and
</li>
<li>
<code>pointer</code> is the same type as <code>element_type*</code>, and
</li>
<li>
<code>UP::pointer</code> is the same type as <code>UP::element_type*</code>, and
</li>
<li>
<code>UP::element_type(*)[]</code> is convertible to <code>element_type(*)[]</code>, and
</li>
<li><del>
either <code>D</code> is a reference type and <code>E</code> is the same type
as <code>D</code>, or <code>D</code> is not a reference type and
<code>E</code> is implicitly convertible to <code>D</code>.
</del></li>
<li><ins>
<code>is_assignable<D&, E&&>::value</code> is true.
</ins></li>
</ul>
<p>
[ <i>Note:</i> this replaces the overload-resolution specification of the primary
template — <i>end note</i> ]
</p>
</blockquote>
</blockquote>
<h2><a name="ComplexExample"></a>Appendix 1: A More Complicated Example</h2>
<p>
Consider a stateful deleter which does nothing but count the number of times it
is used to delete something. Furthermore we want the deleter to be templated
on the (static) type it is deleting.
</p>
<p>
This example is more complicated than it needs to be to just demonstrate the
problem. However I wanted to present an example that was not contrived. This
is intended to be a realistic and motivating example. I did not want to
present a contrived example and be met with arguments such as:
</p>
<blockquote>
<p>
Nobody would ever do that. And if they tried, they would be better off if we
disallowed it.
</p>
</blockquote>
<p>
So if you accept on face value that this is a motivating, non-contrived example,
you do not need to understand the details of this example to understand why
the current proposal breaks this example, nor why the alternative wording
proposed herein fixes it.
</p>
<p>
First the code for the deleter, and then a bit of explanation about it.
</p>
<blockquote><pre>
template <class T>
class deleter
{
<font color="#00A000">// count the number of times this deleter is used</font>
unsigned count_ = 0;
public:
<font color="#00A000">// special members</font>
~deleter() {count_ = std::numeric_limits<unsigned>::max();}
deleter() = default;
deleter(const deleter&) {}
deleter& operator=(const deleter&) {return *this;}
deleter(deleter&& d)
: count_(d.count_)
{
d.count_ = 0;
}
deleter& operator=(deleter&& d)
{
count_ = d.count_;
d.count_ = 0;
return *this;
}
<font color="#00A000">// converting constructors</font>
template <class U,
class = std::enable_if_t<std::is_convertible<U*, T*>::value>>
deleter(const deleter<U>& d)
{}
template <class U,
class = std::enable_if_t<std::is_convertible<U*, T*>::value>>
deleter(deleter<U>&& d)
: count_(d.count_)
{
d.count_ = 0;
}
<font color="#00A000">// deletion operator</font>
void operator()(T* t)
{
delete t;
++count_;
}
<font color="#00A000">// observers</font>
unsigned count() const {return count_;}
friend
std::ostream&
operator<<(std::ostream& os, const deleter& d)
{
return os << d.count_;
}
template <class> friend class deleter;
};
</pre></blockquote>
<p>
The <code>deleter</code> holds a count of the number of times it is used.
For debugging purposes, the destructor sets this count to an unrealistic value,
which is not guaranteed to stick around or be observable after <code>~deleter()</code>,
but often is in practice.
</p>
<p>
An invariant of this class is that even across copies and moves, the total number
of times a delete actually happens is the same as the sum of the counts from all
of the deleters. And that invariant makes the copy members of this class
counter-intuitive. Instead of copying the count, the copy members instead do
not copy anything at all! They simply 0 the lhs in the case of a construction,
or in the case of an assignment, they just don't do anything at all.
</p>
<p>
After such a copy, the invariant is preserved. If instead the counts were
actually copied, then after a copy, the number of deletions as reported by the
sum of all deleters would have doubled.
</p>
<p>
The move members of the <code>deleter</code>, on the other hand, will transfer
the delete counts from the source to the target, zeroing out the source. Thus
the move members also preserve the invariant.
</p>
<p>
Because we want to be able to transfer counts from
<code>deleter<derived></code> to <code>deleter<base></code>,
converting constructors are set up. We set up both a converting copy constructor
and a converting move constructor. These constructors are constrained to allow
converting from <code>deleter<derived></code> to
<code>deleter<base></code> but not vice-versa. We also want to allow
converting assignments. In the interest of keeping this example as simple as
possible, the converting assignments are implicitly handled by the converting
constructors followed by the special member assignment operators.
</p>
<p>
For ease of both writing and of presentation, a using declaration is employed:
</p>
<blockquote><pre>
template <class T>
using Ptr = std::unique_ptr<T, deleter<T>&>;
</pre></blockquote>
<p>
Note the use of the lvalue-reference type for the <code>unique_ptr</code>
deleter. As we are interested in accumulating and observing the state of
these deleters, we are going to collect the data in a couple of "master"
deleters and have all <code>Ptr</code>'s <i>refer</i> to these masters. This
is precisely why <i>reference-deleters</i> were designed into
<code>unique_ptr</code> in the first place.
</p>
<p>
Now lets exercise this code:
</p>
<blockquote><pre>
int
main()
{
deleter<base> db;
deleter<derived> dd;
std::cout << "db = " << db << '\n';
std::cout << "dd = " << dd << '\n';
{
Ptr<derived> pd(new derived, dd);
pd.reset(new derived);
Ptr<base> pb(nullptr, db);
pb = std::move(pd); <font color="#00A000">// The converting move assignment!</font>
std::cout << "pb.get_deleter() = " << pb.get_deleter() << '\n';
std::cout << "pd.get_deleter() = " << pd.get_deleter() << '\n';
}
std::cout << "db = " << db << '\n';
std::cout << "dd = " << dd << '\n';
}
</pre></blockquote>
<p>
Assuming <code>base</code> and <code>derived</code> class types, this code
compiles today (C++11 and C++14), and portably outputs:
</p>
<blockquote><pre>
db = 0
dd = 0
pb.get_deleter() = 0
pd.get_deleter() = 1
db = 1
dd = 1
</pre></blockquote>
<p>
<i>Explanation:</i> The "master" <code>deleter</code>s <code>db</code> and
<code>dd</code> are first constructed. There is one to be used with
<code>unique_ptr<base></code> and one for
<code>unique_ptr<derived></code>. They are both initialized with zero
counts as confirmed by the print statements. Next a scope is opened and a
<code>Ptr<derived></code> is constructed with a <code>new
derived</code> and a reference to <code>dd</code>. At this point
<code>pd</code> owns the pointer and refers to <code>dd</code>, which still
has a count of 0. Next <code>pd</code> is <code>reset</code> with another
non-null <code>derived*</code>. This bumps the count in <code>dd</code> up
to 1. Then a <code>Ptr<base></code> is constructed with
<code>nullptr</code> and a reference to <code>db</code> (whose count is still
zero).
</p>
<p>
Finally we get up to the operation in question: the converting move assignment
operator. This will of course transfer the pointer from <code>pd</code> to
<code>pb</code>, but it also executes:
</p>
<blockquote><pre>
get_deleter() = std::forward<deleter<derived>&>(u.get_deleter());
</pre></blockquote>
<p>
which is a fancy way of saying:
</p>
<blockquote><pre>
get_deleter() = u.get_deleter();
</pre></blockquote>
<p>
That is, the deleters are converting-copy-assigned, which is a no-op, and so
the subsequent print statements confirm that the deleter referred to by
<code>pb</code> still has a count of zero, and the deleter referred to by
<code>pd</code> still has a count of one. Next, scope is exited, causing the
destruction of both <code>Ptr</code>s. Only <code>pb</code> actually calls
its <code>deleter</code> and increments the destructor count. The program
ends with printing out the counts for both master deleters, each with a count
of one.
</p>
<p>
If desired, we could use this same code with by-value deleters:
</p>
<blockquote><pre>
template <class T>
using Ptr = std::unique_ptr<T, deleter<T>>;
</pre></blockquote>
<p>
The result would be a little different, just as valid, but would not demonstrate
the problem with the current proposed resolution to
<a href="http://cplusplus.github.io/LWG/lwg-active.html#2228">LWG 2228</a>,
and thus the use of the reference-deleters in this example.
</p>
<h2><a name="Acknowledgments"></a>Acknowledgments</h2>
<p>
Thanks to Geoffrey Romer for opening
<a href="http://cplusplus.github.io/LWG/lwg-active.html#2228">LWG 2228</a>, and
thus bringing attention to this very real defect.
</p>
</body>
</html>