From Jason Turner

[func.wrap]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmp6itjxzgj/{from.md → to.md} +62 -94
tmp/tmp6itjxzgj/{from.md → to.md} RENAMED
@@ -4,32 +4,28 @@ This subclause describes a polymorphic wrapper class that encapsulates
4
  arbitrary callable objects.
5
 
6
  #### Class `bad_function_call` <a id="func.wrap.badcall">[[func.wrap.badcall]]</a>
7
 
8
  An exception of type `bad_function_call` is thrown by
9
- `function::operator()` ([[func.wrap.func.inv]]) when the function
10
- wrapper object has no target.
11
 
12
  ``` cpp
13
  namespace std {
14
  class bad_function_call : public exception {
15
  public:
16
- // [func.wrap.badcall.const], constructor
17
- bad_function_call() noexcept;
18
  };
19
  }
20
  ```
21
 
22
- ##### `bad_function_call` constructor <a id="func.wrap.badcall.const">[[func.wrap.badcall.const]]</a>
23
-
24
  ``` cpp
25
- bad_function_call() noexcept;
26
  ```
27
 
28
- *Effects:* Constructs a `bad_function_call` object.
29
-
30
- *Postconditions:* `what()` returns an *implementation-defined* NTBS.
31
 
32
  #### Class template `function` <a id="func.wrap.func">[[func.wrap.func]]</a>
33
 
34
  ``` cpp
35
  namespace std {
@@ -42,11 +38,11 @@ namespace std {
42
 
43
  // [func.wrap.func.con], construct/copy/destroy
44
  function() noexcept;
45
  function(nullptr_t) noexcept;
46
  function(const function&);
47
- function(function&&);
48
  template<class F> function(F);
49
 
50
  function& operator=(const function&);
51
  function& operator=(function&&);
52
  function& operator=(nullptr_t) noexcept;
@@ -73,132 +69,116 @@ namespace std {
73
  template<class R, class... ArgTypes>
74
  function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>;
75
 
76
  template<class F> function(F) -> function<see below>;
77
 
78
- // [func.wrap.func.nullptr], Null pointer comparisons
79
  template<class R, class... ArgTypes>
80
  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
81
 
82
- template <class R, class... ArgTypes>
83
- bool operator==(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
84
-
85
- template <class R, class... ArgTypes>
86
- bool operator!=(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
87
-
88
- template <class R, class... ArgTypes>
89
- bool operator!=(nullptr_t, const function<R(ArgTypes...)>&) noexcept;
90
-
91
  // [func.wrap.func.alg], specialized algorithms
92
  template<class R, class... ArgTypes>
93
  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
94
  }
95
  ```
96
 
97
  The `function` class template provides polymorphic wrappers that
98
  generalize the notion of a function pointer. Wrappers can store, copy,
99
- and call arbitrary callable objects ([[func.def]]), given a call
100
- signature ([[func.def]]), allowing functions to be first-class objects.
101
 
102
- A callable type ([[func.def]]) `F` is *Lvalue-Callable* for argument
103
- types `ArgTypes` and return type `R` if the expression
104
  `INVOKE<R>(declval<F&>(), declval<ArgTypes>()...)`, considered as an
105
- unevaluated operand (Clause  [[expr]]), is well formed (
106
- [[func.require]]).
107
 
108
- The `function` class template is a call wrapper ([[func.def]]) whose
109
- call signature ([[func.def]]) is `R(ArgTypes...)`.
110
 
111
  [*Note 1*: The types deduced by the deduction guides for `function` may
112
  change in future versions of this International Standard. — *end note*]
113
 
114
- ##### `function` construct/copy/destroy <a id="func.wrap.func.con">[[func.wrap.func.con]]</a>
115
 
116
  ``` cpp
117
  function() noexcept;
118
  ```
119
 
120
- *Postconditions:* `!*this`.
121
 
122
  ``` cpp
123
  function(nullptr_t) noexcept;
124
  ```
125
 
126
- *Postconditions:* `!*this`.
127
 
128
  ``` cpp
129
  function(const function& f);
130
  ```
131
 
132
- *Postconditions:* `!*this` if `!f`; otherwise, `*this` targets a copy of
133
  `f.target()`.
134
 
135
- *Throws:* shall not throw exceptions if `f`’s target is a specialization
136
- of `reference_wrapper` or a function pointer. Otherwise, may throw
137
  `bad_alloc` or any exception thrown by the copy constructor of the
138
  stored callable object.
139
 
140
- [*Note 1*: Implementations are encouraged to avoid the use of
141
- dynamically allocated memory for small callable objects, for example,
142
- where `f`’s target is an object holding only a pointer or reference to
143
- an object and a member function pointer. — *end note*]
144
 
145
  ``` cpp
146
- function(function&& f);
147
  ```
148
 
149
- *Postconditions:* If `!f`, `*this` has no target; otherwise, the target
150
- of `*this` is equivalent to the target of `f` before the construction,
151
- and `f` is in a valid state with an unspecified value.
152
 
153
- *Throws:* shall not throw exceptions if `f`’s target is a specialization
154
- of `reference_wrapper` or a function pointer. Otherwise, may throw
155
- `bad_alloc` or any exception thrown by the copy or move constructor of
156
- the stored callable object.
157
-
158
- [*Note 2*: Implementations are encouraged to avoid the use of
159
- dynamically allocated memory for small callable objects, for example,
160
- where `f`’s target is an object holding only a pointer or reference to
161
- an object and a member function pointer. — *end note*]
162
 
163
  ``` cpp
164
  template<class F> function(F f);
165
  ```
166
 
167
- *Requires:* `F` shall be `CopyConstructible`.
 
168
 
169
- *Remarks:* This constructor template shall not participate in overload
170
- resolution unless `F` is Lvalue-Callable ([[func.wrap.func]]) for
171
- argument types `ArgTypes...` and return type `R`.
172
 
173
- *Postconditions:* `!*this` if any of the following hold:
174
 
175
  - `f` is a null function pointer value.
176
  - `f` is a null member pointer value.
177
  - `F` is an instance of the `function` class template, and `!f`.
178
 
179
  Otherwise, `*this` targets a copy of `f` initialized with
180
  `std::move(f)`.
181
 
182
- [*Note 3*: Implementations are encouraged to avoid the use of
183
- dynamically allocated memory for small callable objects, for example,
184
- where `f` is an object holding only a pointer or reference to an object
185
- and a member function pointer. — *end note*]
186
 
187
- *Throws:* shall not throw exceptions when `f` is a function pointer or a
188
- `reference_wrapper<T>` for some `T`. Otherwise, may throw `bad_alloc` or
189
- any exception thrown by `F`’s copy or move constructor.
190
 
191
  ``` cpp
192
  template<class F> function(F) -> function<see below>;
193
  ```
194
 
195
- *Remarks:* This deduction guide participates in overload resolution only
196
- if `&F::operator()` is well-formed when treated as an unevaluated
197
- operand. In that case, if `decltype(&F::operator())` is of the form
198
- `R(G::*)(A...)` cv `&`ₒₚₜ ` noexcept` for a class type `G`, then the
199
- deduced type is `function<R(A...)>`.
200
 
201
  [*Example 1*:
202
 
203
  ``` cpp
204
  void f() {
@@ -229,26 +209,25 @@ function& operator=(function&& f);
229
  function& operator=(nullptr_t) noexcept;
230
  ```
231
 
232
  *Effects:* If `*this != nullptr`, destroys the target of `this`.
233
 
234
- *Postconditions:* `!(*this)`.
235
 
236
  *Returns:* `*this`.
237
 
238
  ``` cpp
239
  template<class F> function& operator=(F&& f);
240
  ```
241
 
 
 
 
242
  *Effects:* As if by: `function(std::forward<F>(f)).swap(*this);`
243
 
244
  *Returns:* `*this`.
245
 
246
- *Remarks:* This assignment operator shall not participate in overload
247
- resolution unless `decay_t<F>` is Lvalue-Callable ([[func.wrap.func]])
248
- for argument types `ArgTypes...` and return type `R`.
249
-
250
  ``` cpp
251
  template<class F> function& operator=(reference_wrapper<F> f) noexcept;
252
  ```
253
 
254
  *Effects:* As if by: `function(f).swap(*this);`
@@ -259,40 +238,40 @@ template<class F> function& operator=(reference_wrapper<F> f) noexcept;
259
  ~function();
260
  ```
261
 
262
  *Effects:* If `*this != nullptr`, destroys the target of `this`.
263
 
264
- ##### `function` modifiers <a id="func.wrap.func.mod">[[func.wrap.func.mod]]</a>
265
 
266
  ``` cpp
267
  void swap(function& other) noexcept;
268
  ```
269
 
270
- *Effects:* interchanges the targets of `*this` and `other`.
271
 
272
- ##### `function` capacity <a id="func.wrap.func.cap">[[func.wrap.func.cap]]</a>
273
 
274
  ``` cpp
275
  explicit operator bool() const noexcept;
276
  ```
277
 
278
  *Returns:* `true` if `*this` has a target, otherwise `false`.
279
 
280
- ##### `function` invocation <a id="func.wrap.func.inv">[[func.wrap.func.inv]]</a>
281
 
282
  ``` cpp
283
  R operator()(ArgTypes... args) const;
284
  ```
285
 
286
  *Returns:* *INVOKE*\<R\>(f,
287
- std::forward\<ArgTypes\>(args)...) ([[func.require]]), where `f` is the
288
- target object ([[func.def]]) of `*this`.
289
 
290
  *Throws:* `bad_function_call` if `!*this`; otherwise, any exception
291
  thrown by the wrapped callable object.
292
 
293
- ##### `function` target access <a id="func.wrap.func.targ">[[func.wrap.func.targ]]</a>
294
 
295
  ``` cpp
296
  const type_info& target_type() const noexcept;
297
  ```
298
 
@@ -305,31 +284,20 @@ template<class T> const T* target() const noexcept;
305
  ```
306
 
307
  *Returns:* If `target_type() == typeid(T)` a pointer to the stored
308
  function target; otherwise a null pointer.
309
 
310
- ##### null pointer comparison functions <a id="func.wrap.func.nullptr">[[func.wrap.func.nullptr]]</a>
311
 
312
  ``` cpp
313
  template<class R, class... ArgTypes>
314
  bool operator==(const function<R(ArgTypes...)>& f, nullptr_t) noexcept;
315
- template <class R, class... ArgTypes>
316
- bool operator==(nullptr_t, const function<R(ArgTypes...)>& f) noexcept;
317
  ```
318
 
319
  *Returns:* `!f`.
320
 
321
- ``` cpp
322
- template <class R, class... ArgTypes>
323
- bool operator!=(const function<R(ArgTypes...)>& f, nullptr_t) noexcept;
324
- template <class R, class... ArgTypes>
325
- bool operator!=(nullptr_t, const function<R(ArgTypes...)>& f) noexcept;
326
- ```
327
-
328
- *Returns:* `(bool)f`.
329
-
330
- ##### specialized algorithms <a id="func.wrap.func.alg">[[func.wrap.func.alg]]</a>
331
 
332
  ``` cpp
333
  template<class R, class... ArgTypes>
334
  void swap(function<R(ArgTypes...)>& f1, function<R(ArgTypes...)>& f2) noexcept;
335
  ```
 
4
  arbitrary callable objects.
5
 
6
  #### Class `bad_function_call` <a id="func.wrap.badcall">[[func.wrap.badcall]]</a>
7
 
8
  An exception of type `bad_function_call` is thrown by
9
+ `function::operator()` [[func.wrap.func.inv]] when the function wrapper
10
+ object has no target.
11
 
12
  ``` cpp
13
  namespace std {
14
  class bad_function_call : public exception {
15
  public:
16
+ // see [exception] for the specification of the special member functions
17
+ const char* what() const noexcept override;
18
  };
19
  }
20
  ```
21
 
 
 
22
  ``` cpp
23
+ const char* what() const noexcept override;
24
  ```
25
 
26
+ *Returns:* An *implementation-defined* NTBS.
 
 
27
 
28
  #### Class template `function` <a id="func.wrap.func">[[func.wrap.func]]</a>
29
 
30
  ``` cpp
31
  namespace std {
 
38
 
39
  // [func.wrap.func.con], construct/copy/destroy
40
  function() noexcept;
41
  function(nullptr_t) noexcept;
42
  function(const function&);
43
+ function(function&&) noexcept;
44
  template<class F> function(F);
45
 
46
  function& operator=(const function&);
47
  function& operator=(function&&);
48
  function& operator=(nullptr_t) noexcept;
 
69
  template<class R, class... ArgTypes>
70
  function(R(*)(ArgTypes...)) -> function<R(ArgTypes...)>;
71
 
72
  template<class F> function(F) -> function<see below>;
73
 
74
+ // [func.wrap.func.nullptr], null pointer comparison functions
75
  template<class R, class... ArgTypes>
76
  bool operator==(const function<R(ArgTypes...)>&, nullptr_t) noexcept;
77
 
 
 
 
 
 
 
 
 
 
78
  // [func.wrap.func.alg], specialized algorithms
79
  template<class R, class... ArgTypes>
80
  void swap(function<R(ArgTypes...)>&, function<R(ArgTypes...)>&) noexcept;
81
  }
82
  ```
83
 
84
  The `function` class template provides polymorphic wrappers that
85
  generalize the notion of a function pointer. Wrappers can store, copy,
86
+ and call arbitrary callable objects [[func.def]], given a call signature
87
+ [[func.def]], allowing functions to be first-class objects.
88
 
89
+ A callable type [[func.def]] `F` is *Lvalue-Callable* for argument types
90
+ `ArgTypes` and return type `R` if the expression
91
  `INVOKE<R>(declval<F&>(), declval<ArgTypes>()...)`, considered as an
92
+ unevaluated operand [[expr.prop]], is well-formed [[func.require]].
 
93
 
94
+ The `function` class template is a call wrapper [[func.def]] whose call
95
+ signature [[func.def]] is `R(ArgTypes...)`.
96
 
97
  [*Note 1*: The types deduced by the deduction guides for `function` may
98
  change in future versions of this International Standard. — *end note*]
99
 
100
+ ##### Constructors and destructor <a id="func.wrap.func.con">[[func.wrap.func.con]]</a>
101
 
102
  ``` cpp
103
  function() noexcept;
104
  ```
105
 
106
+ *Ensures:* `!*this`.
107
 
108
  ``` cpp
109
  function(nullptr_t) noexcept;
110
  ```
111
 
112
+ *Ensures:* `!*this`.
113
 
114
  ``` cpp
115
  function(const function& f);
116
  ```
117
 
118
+ *Ensures:* `!*this` if `!f`; otherwise, `*this` targets a copy of
119
  `f.target()`.
120
 
121
+ *Throws:* Nothing if `f`’s target is a specialization of
122
+ `reference_wrapper` or a function pointer. Otherwise, may throw
123
  `bad_alloc` or any exception thrown by the copy constructor of the
124
  stored callable object.
125
 
126
+ [*Note 1*: Implementations should avoid the use of dynamically
127
+ allocated memory for small callable objects, for example, where `f`’s
128
+ target is an object holding only a pointer or reference to an object and
129
+ a member function pointer. — *end note*]
130
 
131
  ``` cpp
132
+ function(function&& f) noexcept;
133
  ```
134
 
135
+ *Ensures:* If `!f`, `*this` has no target; otherwise, the target of
136
+ `*this` is equivalent to the target of `f` before the construction, and
137
+ `f` is in a valid state with an unspecified value.
138
 
139
+ [*Note 2*: Implementations should avoid the use of dynamically
140
+ allocated memory for small callable objects, for example, where `f`’s
141
+ target is an object holding only a pointer or reference to an object and
142
+ a member function pointer. — *end note*]
 
 
 
 
 
143
 
144
  ``` cpp
145
  template<class F> function(F f);
146
  ```
147
 
148
+ *Constraints:* `F` is Lvalue-Callable [[func.wrap.func]] for argument
149
+ types `ArgTypes...` and return type `R`.
150
 
151
+ *Preconditions:* `F` meets the *Cpp17CopyConstructible* requirements.
 
 
152
 
153
+ *Ensures:* `!*this` if any of the following hold:
154
 
155
  - `f` is a null function pointer value.
156
  - `f` is a null member pointer value.
157
  - `F` is an instance of the `function` class template, and `!f`.
158
 
159
  Otherwise, `*this` targets a copy of `f` initialized with
160
  `std::move(f)`.
161
 
162
+ [*Note 3*: Implementations should avoid the use of dynamically
163
+ allocated memory for small callable objects, for example, where `f` is
164
+ an object holding only a pointer or reference to an object and a member
165
+ function pointer. — *end note*]
166
 
167
+ *Throws:* Nothing if `f` is a specialization of `reference_wrapper` or a
168
+ function pointer. Otherwise, may throw `bad_alloc` or any exception
169
+ thrown by `F`’s copy or move constructor.
170
 
171
  ``` cpp
172
  template<class F> function(F) -> function<see below>;
173
  ```
174
 
175
+ *Constraints:* `&F::operator()` is well-formed when treated as an
176
+ unevaluated operand and `decltype(&F::operator())` is of the form
177
+ `R(G::*)(A...)` cv \\ₒₚₜ ` `noexceptₒₚₜ for a class type `G`.
178
+
179
+ *Remarks:* The deduced type is `function<R(A...)>`.
180
 
181
  [*Example 1*:
182
 
183
  ``` cpp
184
  void f() {
 
209
  function& operator=(nullptr_t) noexcept;
210
  ```
211
 
212
  *Effects:* If `*this != nullptr`, destroys the target of `this`.
213
 
214
+ *Ensures:* `!(*this)`.
215
 
216
  *Returns:* `*this`.
217
 
218
  ``` cpp
219
  template<class F> function& operator=(F&& f);
220
  ```
221
 
222
+ *Constraints:* `decay_t<F>` is Lvalue-Callable [[func.wrap.func]] for
223
+ argument types `ArgTypes...` and return type `R`.
224
+
225
  *Effects:* As if by: `function(std::forward<F>(f)).swap(*this);`
226
 
227
  *Returns:* `*this`.
228
 
 
 
 
 
229
  ``` cpp
230
  template<class F> function& operator=(reference_wrapper<F> f) noexcept;
231
  ```
232
 
233
  *Effects:* As if by: `function(f).swap(*this);`
 
238
  ~function();
239
  ```
240
 
241
  *Effects:* If `*this != nullptr`, destroys the target of `this`.
242
 
243
+ ##### Modifiers <a id="func.wrap.func.mod">[[func.wrap.func.mod]]</a>
244
 
245
  ``` cpp
246
  void swap(function& other) noexcept;
247
  ```
248
 
249
+ *Effects:* Interchanges the targets of `*this` and `other`.
250
 
251
+ ##### Capacity <a id="func.wrap.func.cap">[[func.wrap.func.cap]]</a>
252
 
253
  ``` cpp
254
  explicit operator bool() const noexcept;
255
  ```
256
 
257
  *Returns:* `true` if `*this` has a target, otherwise `false`.
258
 
259
+ ##### Invocation <a id="func.wrap.func.inv">[[func.wrap.func.inv]]</a>
260
 
261
  ``` cpp
262
  R operator()(ArgTypes... args) const;
263
  ```
264
 
265
  *Returns:* *INVOKE*\<R\>(f,
266
+ std::forward\<ArgTypes\>(args)...) [[func.require]], where `f` is the
267
+ target object [[func.def]] of `*this`.
268
 
269
  *Throws:* `bad_function_call` if `!*this`; otherwise, any exception
270
  thrown by the wrapped callable object.
271
 
272
+ ##### Target access <a id="func.wrap.func.targ">[[func.wrap.func.targ]]</a>
273
 
274
  ``` cpp
275
  const type_info& target_type() const noexcept;
276
  ```
277
 
 
284
  ```
285
 
286
  *Returns:* If `target_type() == typeid(T)` a pointer to the stored
287
  function target; otherwise a null pointer.
288
 
289
+ ##### Null pointer comparison functions <a id="func.wrap.func.nullptr">[[func.wrap.func.nullptr]]</a>
290
 
291
  ``` cpp
292
  template<class R, class... ArgTypes>
293
  bool operator==(const function<R(ArgTypes...)>& f, nullptr_t) noexcept;
 
 
294
  ```
295
 
296
  *Returns:* `!f`.
297
 
298
+ ##### Specialized algorithms <a id="func.wrap.func.alg">[[func.wrap.func.alg]]</a>
 
 
 
 
 
 
 
 
 
299
 
300
  ``` cpp
301
  template<class R, class... ArgTypes>
302
  void swap(function<R(ArgTypes...)>& f1, function<R(ArgTypes...)>& f2) noexcept;
303
  ```