-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0288-current-draft.bs
511 lines (345 loc) · 18.9 KB
/
0288-current-draft.bs
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
<pre class='metadata'>
Title: any_invocable
Shortname: D0288
Revision: 6
Status: D
Audience: LWG
Group: WG21
URL: https://htmlpreview.github.io/?https://github.com/mattcalabrese/proposal-unique_function/master/0288-current-draft.html
Editor: Matt Calabrese <[email protected]>
Ryan McDougall <[email protected]>
Abstract: This paper proposes a conservative, move-only equivalent of <tt>std::function</tt>.
Date: 2020-07-14
</pre>
Brief History {#History}
========================
This paper started as a proposal by David Krauss, N4543[1], from 2015 and there has
been an open issue in the LEWG bugzilla requesting such a facility since 2014[2].
Since then, the paper has gone through 4 revisions and has been considered in
small groups in LEWG multiple times. Gradual feedback has led to the conservative
proposal seen here. The most-recent draft prior to this was a late-paper written
and presented by Ryan McDougall in LEWGI in San Diego[3]. It included multiple
references to implementations of move-only functions and made a strong case for
the importance of a move-only form of <tt>std::function</tt>.
Feebdack given was encouragement for targetting C++20.
An updated version of that paper was presented on Saturday at the end of the
San Diego meeting. Poll results from that presentation are presented after the
overview in this document.
The revision was presented in Kona, receiving additional polls and feedback, and
was to be forwarded to LWG pending updates reflecting additional poll results. Those
changes have been applied to the wording in this paper. Polls from the LEWG Kona
review are also provided in this document.
Overview {#Overview}
====================
This conservative <tt>any_invocable</tt> is intended to be the same as <tt>std::function</tt>,
with the exceptions of the following:
1. It is move-only.
2. It does not have the const-correctness bug of <tt>std::function</tt> detailed in n4348.[4]
3. It provides support for cv/ref/noexcept qualified function types.
4. It does not have the <tt>target_type</tt> and <tt>target</tt> accessors (direction requested by users and implementors).
4. Invocation has strong preconditions.
Specification {#Specification}
==============================
The following is relative to N4820.[5]
Add <tt><any_invocable></tt> to [tab:headers.cpp] — C++ library headers
Add the following entry to [version.syn#2]:
<tt>#define __cpp_lib_any_invocable xxxxxxL // also in <any_invocable></tt>
Make it so that <tt><functional></tt> is guaranteed to include <tt><any_invocable></tt> by adding the following at the start of the synopsis of [functional.syn]:
<blockquote>
<pre nohighlight>
<b>20.14.1 Header <functional> synopsis [functional.syn]</b>
<tt> <ins>#include <any_invocable> // see [inv.syn]
</ins>
namespace std {
</pre>
</blockquote>
Insert the following section at the end of <b>Function Objects</b> [function.objects], where SECTION is a placeholder for the root of the section numbering:
<blockquote>
<pre nohighlight>
<b>SECTION Storage for any callable [inv]</b>
1 This subclause describes components that C++ programs may use to perform
operations on callable objects of a discriminated type.
<b>SECTION.1 Header <any_invocable> synopsis [inv.syn]</b>
1 The header <tt><any_invocable></tt> provides partial specializations of <tt>any_invocable</tt> for
each combination of the possible replacements of the placeholders <tt><i>cv</i></tt>, <tt><i>ref</i></tt>, and
<tt><i>noex</i></tt> where:
(1.1) — <tt><i>cv</i></tt> is either <tt>const</tt> or empty.
(1.2) — <tt><i>ref</i></tt> is either <tt>&</tt>, <tt>&&</tt>, or empty.
(1.3) — <tt><i>noex</i></tt> is either <tt>true</tt> or <tt>false</tt>.
2 For each of the possible combinations of the placeholders mentioned above, there
is a placeholder <tt><i>inv-quals</i></tt> defined as follows:
(2.1) — If <tt><i>ref</i></tt> is empty, let <tt><i>inv-quals</i></tt> be <tt><i>cv</i>&</tt>
(2.2) — otherwise, let <tt><i>inv-quals</i></tt> be <tt><i>cv ref</i></tt>.
<tt> namespace std {
template<class Sig> class any_invocable; // not defined
template<class R, class... ArgTypes>
class any_invocable<R(ArgTypes...) <i>cv</i> <i>ref</i> noexcept(<i>noex</i>)>;
}</tt>
<b>SECTION.2 Class <any_invocable> [inv.class]</b>
<tt> namespace std {
template<class Sig> class any_invocable; // never defined
template<class R, class... ArgTypes>
class any_invocable<R(ArgTypes...) <i>cv</i> <i>ref</i> noexcept(<i>noex</i>)> {
public:
using result_type = R;
// SECTION.3, construct/copy/destroy
any_invocable() noexcept;
any_invocable(nullptr_t) noexcept;
any_invocable(any_invocable&&) noexcept;
template<class F> any_invocable(F&&);
template<class T, class... Args>
explicit any_invocable(in_place_type_t<T>, Args&&...);
template<class T, class U, class... Args>
explicit any_invocable(in_place_type_t<T>, initializer_list<U>, Args&&...);
any_invocable& operator=(any_invocable&&);
any_invocable& operator=(nullptr_t) noexcept;
template<class F> any_invocable& operator=(F&&);
template<class F> any_invocable& operator=(reference_wrapper<F>) noexcept;
~any_invocable();
// SECTION.4, any_invocable invocation
explicit operator bool() const noexcept;
R operator()(ArgTypes...) <i>cv</i> <i>ref</i> noexcept(<i>noex</i>);
// SECTION.5, any_invocable utility
void swap(any_invocable&) noexcept;
friend void swap(any_invocable&, any_invocable&) noexcept;
friend bool operator==(const any_invocable&, nullptr_t) noexcept;
};
}</tt>
1 The <tt>any_invocable</tt> class template provides polymorphic wrappers that generalize
the notion of a callable object [func.def]. These wrappers can store, move, and
call arbitrary callable objects [func.def], given a call signature [func.def],
allowing functions to be first-class objects.
2 Implementations are encouraged to avoid the use of dynamically allocated memory
for a small contained value.
[ Note: Such small-object optimization can only be applied to types <tt>T</tt> for
which <tt>is_nothrow_move_constructible_v<T></tt> is <tt>true</tt>. -- end note ]
<b>SECTION.3 Constructors and destructor [inv.con]</b>
<tt>any_invocable() noexcept;</tt>
<tt>any_invocable(nullptr_t) noexcept;</tt>
1 <em>Postconditions:</em> <tt>!*this</tt> is <tt>true</tt>.
<tt>any_invocable(any_invocable&& f) noexcept;</tt>
2 <em>Postconditions:</em> If <tt>!f</tt> is <tt>true</tt>, <tt>*this</tt> has no target; otherwise, the target of <tt>*this</tt>
is equivalent to the target of <tt>f</tt> before the construction, and <tt>f</tt> is in a
valid state with an unspecified value.
<tt>template<class F> any_invocable(F&& f);</tt>
3 Let <tt>VT</tt> be <tt>decay_t<F></tt>.
4 <em>Constraints:</em>
(4.1) — <tt>remove_cvref_t<F></tt> is not the same type as <tt>any_invocable</tt>, and
(4.2) — <tt>remove_cvref_t<F></tt> is not a specialization of <tt>in_place_type_t</tt>, and
(4.3) — <tt>is_constructible_v<VT, F></tt> is <tt>true</tt>, and
(4.4) — <tt>is_invocable_r_v<R, VT <i>inv-quals</i>, ArgTypes...></tt> is <tt>true</tt>, and
(4.5) — <tt>!<i>noex</i> || is_nothrow_invocable_r_v<R, VT <i>inv-quals</i>, ArgTypes...></tt> is <tt>true</tt>.
5 <em>Preconditions:</em> <tt>VT</tt> meets the <i>Cpp17Destructible</i> requirements, and if
<tt>is_move_constructible_v<VT></tt> is <tt>true</tt>, <tt>VT</tt> meets the <i>Cpp17MoveConstructible</i>
requirements.
6 <em>Postconditions:</em> <tt>!*this</tt> is <tt>true</tt> if any of the following hold:
(6.1) — <tt>f</tt> is a null function pointer value, or
(6.2) — <tt>f</tt> is a null member pointer value, or
(6.3) — <tt>remove_cvref_t<F></tt> is a specialization of the <tt>any_invocable</tt> class template,
and <tt>!f</tt> is <tt>true</tt>.
7 Otherwise, <tt>*this</tt> targets an object of type <tt>VT</tt> with direct-non-list-initialization
<tt>std::forward<F>(f)</tt>.
8 <em>Throws:</em> Does not throw exceptions when <tt>VT</tt> is a function pointer or a
<tt>reference_wrapper<T></tt> for some <tt>T</tt>. Otherwise, may throw <tt>bad_alloc</tt> or any exception
thrown by the expression <tt>VT(std::forward<F>(f))</tt>.
<tt>template<class T, class... Args>
explicit any_invocable(in_place_type_t<T>, Args&&... args);</tt>
9 Let <tt>VT</tt> be <tt>decay_t<T></tt>.
10 <em>Constraints:</em>
(10.1) — <tt>is_constructible_v<VT, Args...></tt> is <tt>true</tt>, and
(10.2) — <tt>is_invocable_r_v<R, VT <i>inv-quals</i>, ArgTypes...></tt> is <tt>true</tt>, and
(10.3) — <tt>!<i>noex</i> || is_nothrow_invocable_r_v<R, VT <i>inv-quals</i>, ArgTypes...></tt> is <tt>true</tt>.
11 <em>Mandates:</em> <tt>VT</tt> is the same type as <tt>T</tt>.
12 <em>Preconditions:</em> <tt>VT</tt> meets the <i>Cpp17Destructible</i> requirements, and if
<tt>is_move_constructible_v<VT></tt> is <tt>true</tt>, <tt>VT</tt> meets the <i>Cpp17MoveConstructible</i>
requirements.
13 <em>Postconditions:</em> <tt>*this</tt> targets an object of type <tt>VT</tt> initialized with
<tt>std::forward<Args>(args)...</tt> by direct-non-list-initialization.
13 <em>Throws:</em> Does not throw exceptions when <tt>VT</tt> is a function pointer or a
<tt>reference_wrapper<T></tt> for some <tt>T</tt>. Otherwise, may throw <tt>bad_alloc</tt> or any exception
thrown by the expression <tt>VT(std::forward<F>(f))</tt>.
<tt>template<class T, class U, class... Args>
explicit any_invocable(in_place_type_t<T>, initializer_list<U> ilist, Args&&... args);</tt>
14 Let <tt>VT</tt> be <tt>decay_t<T></tt>.
15 <em>Constraints:</em>
(15.1) — <tt>is_constructible_v<VT, initializer_list<U>&, ArgTypes...></tt> is <tt>true</tt>, and
(15.2) — <tt>is_invocable_r_v<R, VT <i>inv-quals</i>, ArgTypes...></tt> is <tt>true</tt>, and
(15.3) — <tt>!<i>noex</i> || is_nothrow_invocable_r_v<R, VT <i>inv-quals</i>, ArgTypes...></tt> is <tt>true</tt>.
16 <em>Mandates:</em> <tt>VT</tt> is the same type as <tt>T</tt>.
17 <em>Preconditions:</em> <tt>VT</tt> meets the <i>Cpp17Destructible</i> requirements, and if
<tt>is_move_constructible_v<VT></tt> is <tt>true</tt>, <tt>VT</tt> meets the <i>Cpp17MoveConstructible</i>
requirements.
18 <em>Postconditions:</em> <tt>*this</tt> targets an object of type <tt>VT</tt> initialized
with <tt>ilist, std::forward<ArgTypes>(args)...</tt> by direct-non-list-initialization.
19 <em>Throws:</em> Does not throw exceptions when <tt>VT</tt> is a function pointer or a
<tt>reference_wrapper<T></tt> for some <tt>T</tt>. Otherwise, may throw <tt>bad_alloc</tt> or any exception
thrown by the expression <tt>VT(std::forward<F>(f))</tt>.
<tt>any_invocable& operator=(any_invocable&& f);</tt>
20 <em>Postconditions:</em> If <tt>!f</tt> is <tt>true</tt>, <tt>*this</tt> has no target; otherwise, the target of <tt>*this</tt>
is equivalent to the target of <tt>f</tt> before the construction, and <tt>f</tt> is in a
valid state with an unspecified value.
21 <em>Returns:</em> <tt>*this</tt>.
<tt>any_invocable& operator=(nullptr_t) noexcept;</tt>
22 <em>Effects:</em> If <tt>*this != nullptr</tt> is <tt>true</tt>, destroys the target object of <tt>this</tt>.
23 <em>Postconditions:</em> <tt>!*this</tt> is <tt>true</tt>.
24 <em>Returns:</em> <tt>*this</tt>.
<tt>template<class F> any_invocable& operator=(F&& f);</tt>
25 <em>Effects:</em> Equivalent to:<tt>
any_invocable(std::forward<F>(f)).swap(*this);
return *this;</tt>
<tt>template<class F> any_invocable& operator=(reference_wrapper<F> f) noexcept;</tt>
26 <em>Effects:</em> Equivalent to:<tt>
any_invocable(f).swap(*this);
return *this;</tt>
<tt>~any_invocable()</tt>;
27 <em>Effects:</em> If <tt>*this != nullptr</tt> is <tt>true</tt>, destroys the target object of <tt>this</tt>.
<b>SECTION.4 Invocation [inv.inv]</b>
<tt>explicit operator bool() const noexcept;</tt>
1 <em>Returns:</em> <tt>true</tt> if *<tt>this</tt> has a target, otherwise <tt>false</tt>.
<tt>R operator()(ArgTypes... args) <i>cv</i> <i>ref</i> noexcept(<i>noex</i>);</tt>
2 <em>Preconditions:</em> <tt>bool(*this)</tt> is <tt>true</tt>.
3 <em>Effects:</em> Equivalent to:
<tt>return INVOKE<R>(static_cast<F <i>inv-quals</i>>(f), std::forward<ArgTypes>(args)...);</tt>
where <tt>f</tt> is the target object of <tt>*this</tt> and <tt>f</tt> has type <tt>F</tt>.
<b>SECTION.5 Utility [inv.util]</b>
<tt>void swap(any_invocable& other) noexcept;</tt>
1 <em>Effects:</em> Exchanges the values of <tt>*this</tt> and <tt>other</tt>.
<tt>void swap(any_invocable& f1, any_invocable& f2) noexcept;</tt>
2 <em>Effects</em>: Equivalent to: <tt>f1.swap(f2)</tt>.
<tt>bool operator==(const any_invocable& f, nullptr_t) noexcept;</tt>
3 <em>Returns:</em> <tt>!f</tt>.
</pre>
</blockquote>
Polls from LEWG San Diego Review (2018) {#SD2018}
=================================================
## Support func(), func() const, func() &&
<pre>
SF F N A SA
6 6 2 1 0
</pre>
## Support func() && only
<pre>
SF F N A SA
2 2 7 1 1
</pre>
## Remove target/target_type
<pre>
SF F N A SA
12 5 0 0 0
</pre>
## Require more stuff (noexcept, const&&, ...)
<pre>
SF F N A SA
0 1 8 6 0
</pre>
Note that the final poll (require more stuff) was not due to members being
against the design, but because we could easily add those facilities in a later
standard without any breakage.
## Name Options
There was one final poll, which brought us to the name <tt>any_invocable</tt>.
<pre>
3 unique_function
3 move_function
2 move_only_function
7 movable_function
8 mfunction
10 any_invocable
8 mofun
8 mofunc
0 callback
4 mvfunction
2 func
0 callable
2 any_function
</pre>
Polls from LEWG Kona Review (2019) {#K2019}
=================================================
## We want to spend time on this now in order to include it in C++20
<pre>
SF F N A SA
8 8 2 0 0
</pre>
## Add support for func() const& and func()&
<pre>
SF F N A SA
0 8 7 0 0
</pre>
## Add support for func() noexcept (x all of the above)
<pre>
SF F N A SA
2 12 2 0 0
</pre>
## Include the option for CTAD
<pre>
SF F N A SA
0 1 5 9 0
</pre>
## Name: callable vs any invocable
<pre>
SC C N AI SAI
3 2 3 5 6
</pre>
## any_invocable vs invocable
<pre>
SAI AI N I SI
3 7 2 5 1
</pre>
## Header choice
<pre>
7 <functional>
11 <any_invocable>
11 <invocable>
3 <()>
</pre>
## Can get std::function from <any_invocable>
<pre>
SF F N A SA
0 1 4 4 7
</pre>
## Can get std::function from <invocable>
<pre>
SF F N A SA
1 3 6 3 2
</pre>
Decide on <any_invocable>. Unanimous for <functional> to pull it in,
even if in its own header.
## Remove the null-check in the call operator and throwing of bad_function_call
<pre>
SF F N A SA
8 2 1 0 0
</pre>
## Remove the null-check in constructors that are not nullptr_t
<pre>
std::any_callable<void(void)> ac = my_ptr_object;
if(ac) { /* true even if my_ptr is nullptr */ }
</pre>
<pre>
SF F N A SA
0 2 2 4 3
</pre>
## Perfect forwarding for converting constructor instead of by-value
Unanimous
## Forward to LWG for C++20
<pre>
SF F N A SA
8 5 0 0 0
</pre>
Implementation Experience {#Impl}
=================================
There are many implementations of a move-only <tt>std::function</tt> with a
design that is similar to this. What is presented is a conservative subset of
those implementations. The changes suggested in LEWG, though minimal, have not
been used in a large codebase.
Previous revisions of this paper have included publicly accessible move-only
function implementations, notably including implementations in HPX, Folly, and
LLVM.
Acknowledgments {#Ack}
======================
Thanks to Tomasz Kamiński, Tim Song, and Nevin Liber for suggestions on wording simplifications.
References {#References}
========================
[1]: David Krauss: N4543 "A polymorphic wrapper for all Callable objects" <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4543.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4543.pdf</a>
[2]: Geoffrey Romer: "Bug 34 - Need type-erased wrappers for move-only callable objects" <a href="https://issues.isocpp.org/show_bug.cgi?id=34">https://issues.isocpp.org/show_bug.cgi?id=34</a>
[3]: Ryan McDougall: P0288R2 "The Need for std::unique_function" <a href="https://wg21.link/p0288r2">https://wg21.link/p0288r2</a>
[4]: Geoffrey Romer: N4348 "Making std::function safe for concurrency" <a href="www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4348.html">www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4348.html</a>
[5]: Richard Smith: N4820 "Working Draft, Standard for Programming Language C++" <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/n4820.pdf</a>