- tmp/tmppopo2hxi/{from.md → to.md} +183 -167
tmp/tmppopo2hxi/{from.md → to.md}
RENAMED
|
@@ -1,8 +1,9 @@
|
|
| 1 |
### Class template `optional` <a id="optional.optional">[[optional.optional]]</a>
|
| 2 |
|
| 3 |
``` cpp
|
|
|
|
| 4 |
template<class T>
|
| 5 |
class optional {
|
| 6 |
public:
|
| 7 |
using value_type = T;
|
| 8 |
|
|
@@ -14,23 +15,23 @@ template <class T>
|
|
| 14 |
template<class... Args>
|
| 15 |
constexpr explicit optional(in_place_t, Args&&...);
|
| 16 |
template<class U, class... Args>
|
| 17 |
constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
|
| 18 |
template<class U = T>
|
| 19 |
-
|
| 20 |
template<class U>
|
| 21 |
-
|
| 22 |
template<class U>
|
| 23 |
-
|
| 24 |
|
| 25 |
// [optional.dtor], destructor
|
| 26 |
~optional();
|
| 27 |
|
| 28 |
// [optional.assign], assignment
|
| 29 |
optional& operator=(nullopt_t) noexcept;
|
| 30 |
-
optional& operator=(const optional&);
|
| 31 |
-
optional& operator=(optional&&) noexcept(see below);
|
| 32 |
template<class U = T> optional& operator=(U&&);
|
| 33 |
template<class U> optional& operator=(const optional<U>&);
|
| 34 |
template<class U> optional& operator=(optional<U>&&);
|
| 35 |
template<class... Args> T& emplace(Args&&...);
|
| 36 |
template<class U, class... Args> T& emplace(initializer_list<U>, Args&&...);
|
|
@@ -59,11 +60,13 @@ template <class T>
|
|
| 59 |
|
| 60 |
private:
|
| 61 |
T *val; // exposition only
|
| 62 |
};
|
| 63 |
|
| 64 |
-
template<class T>
|
|
|
|
|
|
|
| 65 |
```
|
| 66 |
|
| 67 |
Any instance of `optional<T>` at any given time either contains a value
|
| 68 |
or does not contain a value. When an instance of `optional<T>` *contains
|
| 69 |
a value*, it means that an object of type `T`, referred to as the
|
|
@@ -77,135 +80,126 @@ returns `true` if the object contains a value; otherwise the conversion
|
|
| 77 |
returns `false`.
|
| 78 |
|
| 79 |
Member `val` is provided for exposition only. When an `optional<T>`
|
| 80 |
object contains a value, `val` points to the contained value.
|
| 81 |
|
| 82 |
-
`T` shall be
|
| 83 |
-
|
| 84 |
|
| 85 |
#### Constructors <a id="optional.ctor">[[optional.ctor]]</a>
|
| 86 |
|
| 87 |
``` cpp
|
| 88 |
constexpr optional() noexcept;
|
| 89 |
constexpr optional(nullopt_t) noexcept;
|
| 90 |
```
|
| 91 |
|
| 92 |
-
*
|
| 93 |
|
| 94 |
*Remarks:* No contained value is initialized. For every object type `T`
|
| 95 |
-
these constructors
|
| 96 |
|
| 97 |
``` cpp
|
| 98 |
constexpr optional(const optional& rhs);
|
| 99 |
```
|
| 100 |
|
| 101 |
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 102 |
if direct-non-list-initializing an object of type `T` with the
|
| 103 |
expression `*rhs`.
|
| 104 |
|
| 105 |
-
*
|
| 106 |
|
| 107 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 108 |
|
| 109 |
-
*Remarks:* This constructor
|
| 110 |
`is_copy_constructible_v<T>` is `true`. If
|
| 111 |
-
`is_trivially_copy_constructible_v<T>` is `true`, this constructor
|
| 112 |
-
|
| 113 |
|
| 114 |
``` cpp
|
| 115 |
constexpr optional(optional&& rhs) noexcept(see below);
|
| 116 |
```
|
| 117 |
|
|
|
|
|
|
|
| 118 |
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 119 |
if direct-non-list-initializing an object of type `T` with the
|
| 120 |
expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
| 121 |
|
| 122 |
-
*
|
| 123 |
|
| 124 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 125 |
|
| 126 |
*Remarks:* The expression inside `noexcept` is equivalent to
|
| 127 |
-
`is_nothrow_move_constructible_v<T>`.
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
constructor shall be a `constexpr` constructor.
|
| 131 |
|
| 132 |
``` cpp
|
| 133 |
template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);
|
| 134 |
```
|
| 135 |
|
|
|
|
|
|
|
| 136 |
*Effects:* Initializes the contained value as if
|
| 137 |
direct-non-list-initializing an object of type `T` with the arguments
|
| 138 |
`std::forward<Args>(args)...`.
|
| 139 |
|
| 140 |
-
*
|
| 141 |
|
| 142 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 143 |
|
| 144 |
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 145 |
-
constexpr constructor, this constructor
|
| 146 |
-
constructor. This constructor shall not participate in overload
|
| 147 |
-
resolution unless `is_constructible_v<T, Args...>` is `true`.
|
| 148 |
|
| 149 |
``` cpp
|
| 150 |
template<class U, class... Args>
|
| 151 |
constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
|
| 152 |
```
|
| 153 |
|
|
|
|
|
|
|
|
|
|
| 154 |
*Effects:* Initializes the contained value as if
|
| 155 |
direct-non-list-initializing an object of type `T` with the arguments
|
| 156 |
`il, std::forward<Args>(args)...`.
|
| 157 |
|
| 158 |
-
*
|
| 159 |
|
| 160 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 161 |
|
| 162 |
-
*Remarks:*
|
| 163 |
-
|
| 164 |
-
`true`. If `T`’s constructor selected for the initialization is a
|
| 165 |
-
constexpr constructor, this constructor shall be a constexpr
|
| 166 |
-
constructor.
|
| 167 |
-
|
| 168 |
-
[*Note 1*: The following constructors are conditionally specified as
|
| 169 |
-
explicit. This is typically implemented by declaring two such
|
| 170 |
-
constructors, of which at most one participates in overload
|
| 171 |
-
resolution. — *end note*]
|
| 172 |
|
| 173 |
``` cpp
|
| 174 |
-
template
|
| 175 |
```
|
| 176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
*Effects:* Initializes the contained value as if
|
| 178 |
direct-non-list-initializing an object of type `T` with the expression
|
| 179 |
`std::forward<U>(v)`.
|
| 180 |
|
| 181 |
-
*
|
| 182 |
|
| 183 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 184 |
|
| 185 |
*Remarks:* If `T`’s selected constructor is a constexpr constructor,
|
| 186 |
-
this constructor
|
| 187 |
-
|
| 188 |
-
`is_constructible_v<T, U&&>` is `true`,
|
| 189 |
-
`is_same_v<decay_t<U>, in_place_t>` is `false`, and
|
| 190 |
-
`is_same_v<optional<T>, decay_t<U>>` is `false`. The constructor is
|
| 191 |
-
explicit if and only if `is_convertible_v<U&&, T>` is `false`.
|
| 192 |
|
| 193 |
``` cpp
|
| 194 |
-
|
| 195 |
```
|
| 196 |
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
*Postconditions:* `bool(rhs)` == `bool(*this)`.
|
| 202 |
-
|
| 203 |
-
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 204 |
|
| 205 |
-
*
|
| 206 |
-
unless
|
| 207 |
|
| 208 |
- `is_constructible_v<T, const U&>` is `true`,
|
| 209 |
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 210 |
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
| 211 |
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
|
@@ -213,40 +207,53 @@ unless
|
|
| 213 |
- `is_convertible_v<optional<U>&, T>` is `false`,
|
| 214 |
- `is_convertible_v<optional<U>&&, T>` is `false`,
|
| 215 |
- `is_convertible_v<const optional<U>&, T>` is `false`, and
|
| 216 |
- `is_convertible_v<const optional<U>&&, T>` is `false`.
|
| 217 |
|
| 218 |
-
|
| 219 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 220 |
|
| 221 |
``` cpp
|
| 222 |
-
template
|
| 223 |
```
|
| 224 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 226 |
if direct-non-list-initializing an object of type `T` with the
|
| 227 |
expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
| 228 |
|
| 229 |
-
*
|
| 230 |
|
| 231 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 232 |
|
| 233 |
-
*Remarks:*
|
| 234 |
-
unless
|
| 235 |
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
| 240 |
-
- `is_constructible_v<T, const optional<U>&&>` is `false`,
|
| 241 |
-
- `is_convertible_v<optional<U>&, T>` is `false`,
|
| 242 |
-
- `is_convertible_v<optional<U>&&, T>` is `false`,
|
| 243 |
-
- `is_convertible_v<const optional<U>&, T>` is `false`, and
|
| 244 |
-
- `is_convertible_v<const optional<U>&&, T>` is `false`.
|
| 245 |
-
|
| 246 |
-
The constructor is explicit if and only if `is_convertible_v<U&&, T>` is
|
| 247 |
-
`false`.
|
| 248 |
|
| 249 |
#### Destructor <a id="optional.dtor">[[optional.dtor]]</a>
|
| 250 |
|
| 251 |
``` cpp
|
| 252 |
~optional();
|
|
@@ -257,72 +264,77 @@ contains a value, calls
|
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
val->T::~T()
|
| 260 |
```
|
| 261 |
|
| 262 |
-
*Remarks:* If `is_trivially_destructible_v<T>
|
| 263 |
-
destructor
|
| 264 |
|
| 265 |
#### Assignment <a id="optional.assign">[[optional.assign]]</a>
|
| 266 |
|
| 267 |
``` cpp
|
| 268 |
optional<T>& operator=(nullopt_t) noexcept;
|
| 269 |
```
|
| 270 |
|
| 271 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 272 |
the contained value; otherwise no effect.
|
| 273 |
|
| 274 |
-
*
|
| 275 |
|
| 276 |
-
*
|
| 277 |
|
| 278 |
``` cpp
|
| 279 |
-
optional<T>& operator=(const optional& rhs);
|
| 280 |
```
|
| 281 |
|
| 282 |
-
*Effects:* See
|
| 283 |
|
| 284 |
-
**Table: `optional::operator=(const optional&)` effects** <a id="
|
| 285 |
|
| 286 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 287 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
|
| 288 |
| `rhs` contains a value | assigns `*rhs` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `*rhs` |
|
| 289 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 290 |
|
| 291 |
|
| 292 |
-
*
|
| 293 |
|
| 294 |
-
*
|
| 295 |
|
| 296 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 297 |
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 298 |
call to `T`’s copy constructor, no effect. If an exception is thrown
|
| 299 |
during the call to `T`’s copy assignment, the state of its contained
|
| 300 |
value is as defined by the exception safety guarantee of `T`’s copy
|
| 301 |
-
assignment. This operator
|
| 302 |
`is_copy_constructible_v<T>` is `true` and `is_copy_assignable_v<T>` is
|
| 303 |
-
`true`.
|
|
|
|
|
|
|
| 304 |
|
| 305 |
``` cpp
|
| 306 |
-
optional
|
| 307 |
```
|
| 308 |
|
| 309 |
-
*
|
| 310 |
-
|
| 311 |
|
| 312 |
-
*
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 315 |
| ------------------------------ | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
|
| 316 |
| `rhs` contains a value | assigns `std::move(*rhs)` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `std::move(*rhs)` |
|
| 317 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 318 |
|
| 319 |
|
|
|
|
|
|
|
| 320 |
*Returns:* `*this`.
|
| 321 |
|
| 322 |
-
*Postconditions:* `bool(rhs) == bool(*this)`.
|
| 323 |
-
|
| 324 |
*Remarks:* The expression inside `noexcept` is equivalent to:
|
| 325 |
|
| 326 |
``` cpp
|
| 327 |
is_nothrow_move_assignable_v<T> && is_nothrow_move_constructible_v<T>
|
| 328 |
```
|
|
@@ -331,65 +343,44 @@ If any exception is thrown, the result of the expression `bool(*this)`
|
|
| 331 |
remains unchanged. If an exception is thrown during the call to `T`’s
|
| 332 |
move constructor, the state of `*rhs.val` is determined by the exception
|
| 333 |
safety guarantee of `T`’s move constructor. If an exception is thrown
|
| 334 |
during the call to `T`’s move assignment, the state of `*val` and
|
| 335 |
`*rhs.val` is determined by the exception safety guarantee of `T`’s move
|
| 336 |
-
assignment.
|
| 337 |
-
|
| 338 |
-
|
| 339 |
|
| 340 |
``` cpp
|
| 341 |
template<class U = T> optional<T>& operator=(U&& v);
|
| 342 |
```
|
| 343 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 344 |
*Effects:* If `*this` contains a value, assigns `std::forward<U>(v)` to
|
| 345 |
the contained value; otherwise initializes the contained value as if
|
| 346 |
direct-non-list-initializing object of type `T` with
|
| 347 |
`std::forward<U>(v)`.
|
| 348 |
|
| 349 |
-
*
|
| 350 |
|
| 351 |
-
*
|
| 352 |
|
| 353 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 354 |
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 355 |
call to `T`’s constructor, the state of `v` is determined by the
|
| 356 |
exception safety guarantee of `T`’s constructor. If an exception is
|
| 357 |
thrown during the call to `T`’s assignment, the state of `*val` and `v`
|
| 358 |
is determined by the exception safety guarantee of `T`’s assignment.
|
| 359 |
-
This function shall not participate in overload resolution unless
|
| 360 |
-
`is_same_v<optional<T>, decay_t<U>>` is `false`,
|
| 361 |
-
`conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>` is `false`,
|
| 362 |
-
`is_constructible_v<T, U>` is `true`, and `is_assignable_v<T&, U>` is
|
| 363 |
-
`true`.
|
| 364 |
|
| 365 |
``` cpp
|
| 366 |
template<class U> optional<T>& operator=(const optional<U>& rhs);
|
| 367 |
```
|
| 368 |
|
| 369 |
-
*
|
| 370 |
-
|
| 371 |
-
**Table: `optional::operator=(const optional<U>&)` effects** <a id="tab:optional.assign.copy.templ">[tab:optional.assign.copy.templ]</a>
|
| 372 |
-
|
| 373 |
-
| | `*this` contains a value | `*this` does not contain a value |
|
| 374 |
-
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
|
| 375 |
-
| `rhs` contains a value | assigns `*rhs` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `*rhs` |
|
| 376 |
-
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
*Returns:* `*this`.
|
| 380 |
-
|
| 381 |
-
*Postconditions:* `bool(rhs) == bool(*this)`.
|
| 382 |
-
|
| 383 |
-
*Remarks:* If any exception is thrown, the result of the expression
|
| 384 |
-
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 385 |
-
call to `T`’s constructor, the state of `*rhs.val` is determined by the
|
| 386 |
-
exception safety guarantee of `T`’s constructor. If an exception is
|
| 387 |
-
thrown during the call to `T`’s assignment, the state of `*val` and
|
| 388 |
-
`*rhs.val` is determined by the exception safety guarantee of `T`’s
|
| 389 |
-
assignment. This function shall not participate in overload resolution
|
| 390 |
-
unless
|
| 391 |
|
| 392 |
- `is_constructible_v<T, const U&>` is `true`,
|
| 393 |
- `is_assignable_v<T&, const U&>` is `true`,
|
| 394 |
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 395 |
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
|
@@ -402,37 +393,37 @@ unless
|
|
| 402 |
- `is_assignable_v<T&, optional<U>&>` is `false`,
|
| 403 |
- `is_assignable_v<T&, optional<U>&&>` is `false`,
|
| 404 |
- `is_assignable_v<T&, const optional<U>&>` is `false`, and
|
| 405 |
- `is_assignable_v<T&, const optional<U>&&>` is `false`.
|
| 406 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 407 |
``` cpp
|
| 408 |
template<class U> optional<T>& operator=(optional<U>&& rhs);
|
| 409 |
```
|
| 410 |
|
| 411 |
-
*
|
| 412 |
-
the expression `bool(rhs)` remains unchanged.
|
| 413 |
-
|
| 414 |
-
**Table: `optional::operator=(optional<U>&&)` effects** <a id="tab:optional.assign.move.templ">[tab:optional.assign.move.templ]</a>
|
| 415 |
-
|
| 416 |
-
| | `*this` contains a value | `*this` does not contain a value |
|
| 417 |
-
| ------------------------------ | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
|
| 418 |
-
| `rhs` contains a value | assigns `std::move(*rhs)` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `std::move(*rhs)` |
|
| 419 |
-
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
*Returns:* `*this`.
|
| 423 |
-
|
| 424 |
-
*Postconditions:* `bool(rhs) == bool(*this)`.
|
| 425 |
-
|
| 426 |
-
*Remarks:* If any exception is thrown, the result of the expression
|
| 427 |
-
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 428 |
-
call to `T`’s constructor, the state of `*rhs.val` is determined by the
|
| 429 |
-
exception safety guarantee of `T`’s constructor. If an exception is
|
| 430 |
-
thrown during the call to `T`’s assignment, the state of `*val` and
|
| 431 |
-
`*rhs.val` is determined by the exception safety guarantee of `T`’s
|
| 432 |
-
assignment. This function shall not participate in overload resolution
|
| 433 |
-
unless
|
| 434 |
|
| 435 |
- `is_constructible_v<T, U>` is `true`,
|
| 436 |
- `is_assignable_v<T&, U>` is `true`,
|
| 437 |
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 438 |
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
|
@@ -445,21 +436,44 @@ unless
|
|
| 445 |
- `is_assignable_v<T&, optional<U>&>` is `false`,
|
| 446 |
- `is_assignable_v<T&, optional<U>&&>` is `false`,
|
| 447 |
- `is_assignable_v<T&, const optional<U>&>` is `false`, and
|
| 448 |
- `is_assignable_v<T&, const optional<U>&&>` is `false`.
|
| 449 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
``` cpp
|
| 451 |
template<class... Args> T& emplace(Args&&... args);
|
| 452 |
```
|
| 453 |
|
| 454 |
-
*
|
| 455 |
|
| 456 |
*Effects:* Calls `*this = nullopt`. Then initializes the contained value
|
| 457 |
as if direct-non-list-initializing an object of type `T` with the
|
| 458 |
arguments `std::forward<Args>(args)...`.
|
| 459 |
|
| 460 |
-
*
|
| 461 |
|
| 462 |
*Returns:* A reference to the new contained value.
|
| 463 |
|
| 464 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 465 |
|
|
@@ -469,47 +483,49 @@ constructor, `*this` does not contain a value, and the previous `*val`
|
|
| 469 |
|
| 470 |
``` cpp
|
| 471 |
template<class U, class... Args> T& emplace(initializer_list<U> il, Args&&... args);
|
| 472 |
```
|
| 473 |
|
|
|
|
|
|
|
|
|
|
| 474 |
*Effects:* Calls `*this = nullopt`. Then initializes the contained value
|
| 475 |
as if direct-non-list-initializing an object of type `T` with the
|
| 476 |
arguments `il, std::forward<Args>(args)...`.
|
| 477 |
|
| 478 |
-
*
|
| 479 |
|
| 480 |
*Returns:* A reference to the new contained value.
|
| 481 |
|
| 482 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 483 |
|
| 484 |
*Remarks:* If an exception is thrown during the call to `T`’s
|
| 485 |
constructor, `*this` does not contain a value, and the previous `*val`
|
| 486 |
-
(if any) has been destroyed.
|
| 487 |
-
overload resolution unless
|
| 488 |
-
`is_constructible_v<T, initializer_list<U>&, Args&&...>` is `true`.
|
| 489 |
|
| 490 |
#### Swap <a id="optional.swap">[[optional.swap]]</a>
|
| 491 |
|
| 492 |
``` cpp
|
| 493 |
void swap(optional& rhs) noexcept(see below);
|
| 494 |
```
|
| 495 |
|
| 496 |
-
*
|
| 497 |
-
`is_move_constructible_v<T>` is `true`.
|
| 498 |
|
| 499 |
-
*
|
| 500 |
|
| 501 |
-
*
|
|
|
|
|
|
|
| 502 |
|
| 503 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 504 |
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| 505 |
| `rhs` contains a value | calls `swap(*(*this), *rhs)` | initializes the contained value of `*this` as if direct-non-list-initializing an object of type `T` with the expression `std::move(*rhs)`, followed by `rhs.val->T::~T()`; postcondition is that `*this` contains a value and `rhs` does not contain a value |
|
| 506 |
| `rhs` does not contain a value | initializes the contained value of `rhs` as if direct-non-list-initializing an object of type `T` with the expression `std::move(*(*this))`, followed by `val->T::~T()`; postcondition is that `*this` does not contain a value and `rhs` contains a value | no effect |
|
| 507 |
|
| 508 |
|
| 509 |
*Throws:* Any exceptions thrown by the operations in the relevant part
|
| 510 |
-
of
|
| 511 |
|
| 512 |
*Remarks:* The expression inside `noexcept` is equivalent to:
|
| 513 |
|
| 514 |
``` cpp
|
| 515 |
is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T>
|
|
@@ -528,55 +544,55 @@ exception safety guarantee of `T`’s move constructor.
|
|
| 528 |
``` cpp
|
| 529 |
constexpr const T* operator->() const;
|
| 530 |
constexpr T* operator->();
|
| 531 |
```
|
| 532 |
|
| 533 |
-
*
|
| 534 |
|
| 535 |
*Returns:* `val`.
|
| 536 |
|
| 537 |
*Throws:* Nothing.
|
| 538 |
|
| 539 |
-
*Remarks:* These functions
|
| 540 |
|
| 541 |
``` cpp
|
| 542 |
constexpr const T& operator*() const&;
|
| 543 |
constexpr T& operator*() &;
|
| 544 |
```
|
| 545 |
|
| 546 |
-
*
|
| 547 |
|
| 548 |
*Returns:* `*val`.
|
| 549 |
|
| 550 |
*Throws:* Nothing.
|
| 551 |
|
| 552 |
-
*Remarks:* These functions
|
| 553 |
|
| 554 |
``` cpp
|
| 555 |
constexpr T&& operator*() &&;
|
| 556 |
constexpr const T&& operator*() const&&;
|
| 557 |
```
|
| 558 |
|
| 559 |
-
*
|
| 560 |
|
| 561 |
*Effects:* Equivalent to: `return std::move(*val);`
|
| 562 |
|
| 563 |
``` cpp
|
| 564 |
constexpr explicit operator bool() const noexcept;
|
| 565 |
```
|
| 566 |
|
| 567 |
*Returns:* `true` if and only if `*this` contains a value.
|
| 568 |
|
| 569 |
-
*Remarks:* This function
|
| 570 |
|
| 571 |
``` cpp
|
| 572 |
constexpr bool has_value() const noexcept;
|
| 573 |
```
|
| 574 |
|
| 575 |
*Returns:* `true` if and only if `*this` contains a value.
|
| 576 |
|
| 577 |
-
*Remarks:* This function
|
| 578 |
|
| 579 |
``` cpp
|
| 580 |
constexpr const T& value() const&;
|
| 581 |
constexpr T& value() &;
|
| 582 |
```
|
|
@@ -600,38 +616,38 @@ return bool(*this) ? std::move(*val) : throw bad_optional_access();
|
|
| 600 |
|
| 601 |
``` cpp
|
| 602 |
template<class U> constexpr T value_or(U&& v) const&;
|
| 603 |
```
|
| 604 |
|
|
|
|
|
|
|
|
|
|
| 605 |
*Effects:* Equivalent to:
|
| 606 |
|
| 607 |
``` cpp
|
| 608 |
return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
|
| 609 |
```
|
| 610 |
|
| 611 |
-
*Remarks:* If `is_copy_constructible_v<T> && is_convertible_v<U&&, T>`
|
| 612 |
-
is `false`, the program is ill-formed.
|
| 613 |
-
|
| 614 |
``` cpp
|
| 615 |
template<class U> constexpr T value_or(U&& v) &&;
|
| 616 |
```
|
| 617 |
|
|
|
|
|
|
|
|
|
|
| 618 |
*Effects:* Equivalent to:
|
| 619 |
|
| 620 |
``` cpp
|
| 621 |
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
| 622 |
```
|
| 623 |
|
| 624 |
-
*Remarks:* If `is_move_constructible_v<T> && is_convertible_v<U&&, T>`
|
| 625 |
-
is `false`, the program is ill-formed.
|
| 626 |
-
|
| 627 |
#### Modifiers <a id="optional.mod">[[optional.mod]]</a>
|
| 628 |
|
| 629 |
``` cpp
|
| 630 |
void reset() noexcept;
|
| 631 |
```
|
| 632 |
|
| 633 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 634 |
the contained value; otherwise no effect.
|
| 635 |
|
| 636 |
-
*
|
| 637 |
|
|
|
|
| 1 |
### Class template `optional` <a id="optional.optional">[[optional.optional]]</a>
|
| 2 |
|
| 3 |
``` cpp
|
| 4 |
+
namespace std {
|
| 5 |
template<class T>
|
| 6 |
class optional {
|
| 7 |
public:
|
| 8 |
using value_type = T;
|
| 9 |
|
|
|
|
| 15 |
template<class... Args>
|
| 16 |
constexpr explicit optional(in_place_t, Args&&...);
|
| 17 |
template<class U, class... Args>
|
| 18 |
constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
|
| 19 |
template<class U = T>
|
| 20 |
+
constexpr explicit(see below) optional(U&&);
|
| 21 |
template<class U>
|
| 22 |
+
explicit(see below) optional(const optional<U>&);
|
| 23 |
template<class U>
|
| 24 |
+
explicit(see below) optional(optional<U>&&);
|
| 25 |
|
| 26 |
// [optional.dtor], destructor
|
| 27 |
~optional();
|
| 28 |
|
| 29 |
// [optional.assign], assignment
|
| 30 |
optional& operator=(nullopt_t) noexcept;
|
| 31 |
+
constexpr optional& operator=(const optional&);
|
| 32 |
+
constexpr optional& operator=(optional&&) noexcept(see below);
|
| 33 |
template<class U = T> optional& operator=(U&&);
|
| 34 |
template<class U> optional& operator=(const optional<U>&);
|
| 35 |
template<class U> optional& operator=(optional<U>&&);
|
| 36 |
template<class... Args> T& emplace(Args&&...);
|
| 37 |
template<class U, class... Args> T& emplace(initializer_list<U>, Args&&...);
|
|
|
|
| 60 |
|
| 61 |
private:
|
| 62 |
T *val; // exposition only
|
| 63 |
};
|
| 64 |
|
| 65 |
+
template<class T>
|
| 66 |
+
optional(T) -> optional<T>;
|
| 67 |
+
}
|
| 68 |
```
|
| 69 |
|
| 70 |
Any instance of `optional<T>` at any given time either contains a value
|
| 71 |
or does not contain a value. When an instance of `optional<T>` *contains
|
| 72 |
a value*, it means that an object of type `T`, referred to as the
|
|
|
|
| 80 |
returns `false`.
|
| 81 |
|
| 82 |
Member `val` is provided for exposition only. When an `optional<T>`
|
| 83 |
object contains a value, `val` points to the contained value.
|
| 84 |
|
| 85 |
+
`T` shall be a type other than cv `in_place_t` or cv `nullopt_t` that
|
| 86 |
+
meets the *Cpp17Destructible* requirements ([[cpp17.destructible]]).
|
| 87 |
|
| 88 |
#### Constructors <a id="optional.ctor">[[optional.ctor]]</a>
|
| 89 |
|
| 90 |
``` cpp
|
| 91 |
constexpr optional() noexcept;
|
| 92 |
constexpr optional(nullopt_t) noexcept;
|
| 93 |
```
|
| 94 |
|
| 95 |
+
*Ensures:* `*this` does not contain a value.
|
| 96 |
|
| 97 |
*Remarks:* No contained value is initialized. For every object type `T`
|
| 98 |
+
these constructors are constexpr constructors [[dcl.constexpr]].
|
| 99 |
|
| 100 |
``` cpp
|
| 101 |
constexpr optional(const optional& rhs);
|
| 102 |
```
|
| 103 |
|
| 104 |
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 105 |
if direct-non-list-initializing an object of type `T` with the
|
| 106 |
expression `*rhs`.
|
| 107 |
|
| 108 |
+
*Ensures:* `bool(rhs) == bool(*this)`.
|
| 109 |
|
| 110 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 111 |
|
| 112 |
+
*Remarks:* This constructor is defined as deleted unless
|
| 113 |
`is_copy_constructible_v<T>` is `true`. If
|
| 114 |
+
`is_trivially_copy_constructible_v<T>` is `true`, this constructor is
|
| 115 |
+
trivial.
|
| 116 |
|
| 117 |
``` cpp
|
| 118 |
constexpr optional(optional&& rhs) noexcept(see below);
|
| 119 |
```
|
| 120 |
|
| 121 |
+
*Constraints:* `is_move_constructible_v<T>` is `true`.
|
| 122 |
+
|
| 123 |
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 124 |
if direct-non-list-initializing an object of type `T` with the
|
| 125 |
expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
| 126 |
|
| 127 |
+
*Ensures:* `bool(rhs) == bool(*this)`.
|
| 128 |
|
| 129 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 130 |
|
| 131 |
*Remarks:* The expression inside `noexcept` is equivalent to
|
| 132 |
+
`is_nothrow_move_constructible_v<T>`. If
|
| 133 |
+
`is_trivially_move_constructible_v<T>` is `true`, this constructor is
|
| 134 |
+
trivial.
|
|
|
|
| 135 |
|
| 136 |
``` cpp
|
| 137 |
template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);
|
| 138 |
```
|
| 139 |
|
| 140 |
+
*Constraints:* `is_constructible_v<T, Args...>` is `true`.
|
| 141 |
+
|
| 142 |
*Effects:* Initializes the contained value as if
|
| 143 |
direct-non-list-initializing an object of type `T` with the arguments
|
| 144 |
`std::forward<Args>(args)...`.
|
| 145 |
|
| 146 |
+
*Ensures:* `*this` contains a value.
|
| 147 |
|
| 148 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 149 |
|
| 150 |
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 151 |
+
constexpr constructor, this constructor is a constexpr constructor.
|
|
|
|
|
|
|
| 152 |
|
| 153 |
``` cpp
|
| 154 |
template<class U, class... Args>
|
| 155 |
constexpr explicit optional(in_place_t, initializer_list<U> il, Args&&... args);
|
| 156 |
```
|
| 157 |
|
| 158 |
+
*Constraints:* `is_constructible_v<T, initializer_list<U>&, Args...>` is
|
| 159 |
+
`true`.
|
| 160 |
+
|
| 161 |
*Effects:* Initializes the contained value as if
|
| 162 |
direct-non-list-initializing an object of type `T` with the arguments
|
| 163 |
`il, std::forward<Args>(args)...`.
|
| 164 |
|
| 165 |
+
*Ensures:* `*this` contains a value.
|
| 166 |
|
| 167 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 168 |
|
| 169 |
+
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 170 |
+
constexpr constructor, this constructor is a constexpr constructor.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
``` cpp
|
| 173 |
+
template<class U = T> constexpr explicit(see below) optional(U&& v);
|
| 174 |
```
|
| 175 |
|
| 176 |
+
*Constraints:* `is_constructible_v<T, U>` is `true`,
|
| 177 |
+
`is_same_v<remove_cvref_t<U>, in_place_t>` is `false`, and
|
| 178 |
+
`is_same_v<remove_cvref_t<U>, optional>` is `false`.
|
| 179 |
+
|
| 180 |
*Effects:* Initializes the contained value as if
|
| 181 |
direct-non-list-initializing an object of type `T` with the expression
|
| 182 |
`std::forward<U>(v)`.
|
| 183 |
|
| 184 |
+
*Ensures:* `*this` contains a value.
|
| 185 |
|
| 186 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 187 |
|
| 188 |
*Remarks:* If `T`’s selected constructor is a constexpr constructor,
|
| 189 |
+
this constructor is a constexpr constructor. The expression inside
|
| 190 |
+
`explicit` is equivalent to:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
|
| 192 |
``` cpp
|
| 193 |
+
!is_convertible_v<U, T>
|
| 194 |
```
|
| 195 |
|
| 196 |
+
``` cpp
|
| 197 |
+
template<class U> explicit(see below) optional(const optional<U>& rhs);
|
| 198 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
*Constraints:*
|
|
|
|
| 201 |
|
| 202 |
- `is_constructible_v<T, const U&>` is `true`,
|
| 203 |
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 204 |
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
| 205 |
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
|
|
|
| 207 |
- `is_convertible_v<optional<U>&, T>` is `false`,
|
| 208 |
- `is_convertible_v<optional<U>&&, T>` is `false`,
|
| 209 |
- `is_convertible_v<const optional<U>&, T>` is `false`, and
|
| 210 |
- `is_convertible_v<const optional<U>&&, T>` is `false`.
|
| 211 |
|
| 212 |
+
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 213 |
+
if direct-non-list-initializing an object of type `T` with the
|
| 214 |
+
expression `*rhs`.
|
| 215 |
+
|
| 216 |
+
*Ensures:* `bool(rhs)` == `bool(*this)`.
|
| 217 |
+
|
| 218 |
+
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 219 |
+
|
| 220 |
+
*Remarks:* The expression inside `explicit` is equivalent to:
|
| 221 |
+
|
| 222 |
+
``` cpp
|
| 223 |
+
!is_convertible_v<const U&, T>
|
| 224 |
+
```
|
| 225 |
|
| 226 |
``` cpp
|
| 227 |
+
template<class U> explicit(see below) optional(optional<U>&& rhs);
|
| 228 |
```
|
| 229 |
|
| 230 |
+
*Constraints:*
|
| 231 |
+
|
| 232 |
+
- `is_constructible_v<T, U>` is true,
|
| 233 |
+
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 234 |
+
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
| 235 |
+
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
| 236 |
+
- `is_constructible_v<T, const optional<U>&&>` is `false`,
|
| 237 |
+
- `is_convertible_v<optional<U>&, T>` is `false`,
|
| 238 |
+
- `is_convertible_v<optional<U>&&, T>` is `false`,
|
| 239 |
+
- `is_convertible_v<const optional<U>&, T>` is `false`, and
|
| 240 |
+
- `is_convertible_v<const optional<U>&&, T>` is `false`.
|
| 241 |
+
|
| 242 |
*Effects:* If `rhs` contains a value, initializes the contained value as
|
| 243 |
if direct-non-list-initializing an object of type `T` with the
|
| 244 |
expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
| 245 |
|
| 246 |
+
*Ensures:* `bool(rhs)` == `bool(*this)`.
|
| 247 |
|
| 248 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 249 |
|
| 250 |
+
*Remarks:* The expression inside `explicit` is equivalent to:
|
|
|
|
| 251 |
|
| 252 |
+
``` cpp
|
| 253 |
+
!is_convertible_v<U, T>
|
| 254 |
+
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
#### Destructor <a id="optional.dtor">[[optional.dtor]]</a>
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
~optional();
|
|
|
|
| 264 |
|
| 265 |
``` cpp
|
| 266 |
val->T::~T()
|
| 267 |
```
|
| 268 |
|
| 269 |
+
*Remarks:* If `is_trivially_destructible_v<T>` is `true`, then this
|
| 270 |
+
destructor is trivial.
|
| 271 |
|
| 272 |
#### Assignment <a id="optional.assign">[[optional.assign]]</a>
|
| 273 |
|
| 274 |
``` cpp
|
| 275 |
optional<T>& operator=(nullopt_t) noexcept;
|
| 276 |
```
|
| 277 |
|
| 278 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 279 |
the contained value; otherwise no effect.
|
| 280 |
|
| 281 |
+
*Ensures:* `*this` does not contain a value.
|
| 282 |
|
| 283 |
+
*Returns:* `*this`.
|
| 284 |
|
| 285 |
``` cpp
|
| 286 |
+
constexpr optional<T>& operator=(const optional& rhs);
|
| 287 |
```
|
| 288 |
|
| 289 |
+
*Effects:* See [[optional.assign.copy]].
|
| 290 |
|
| 291 |
+
**Table: `optional::operator=(const optional&)` effects** <a id="optional.assign.copy">[optional.assign.copy]</a>
|
| 292 |
|
| 293 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 294 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
|
| 295 |
| `rhs` contains a value | assigns `*rhs` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `*rhs` |
|
| 296 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 297 |
|
| 298 |
|
| 299 |
+
*Ensures:* `bool(rhs) == bool(*this)`.
|
| 300 |
|
| 301 |
+
*Returns:* `*this`.
|
| 302 |
|
| 303 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 304 |
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 305 |
call to `T`’s copy constructor, no effect. If an exception is thrown
|
| 306 |
during the call to `T`’s copy assignment, the state of its contained
|
| 307 |
value is as defined by the exception safety guarantee of `T`’s copy
|
| 308 |
+
assignment. This operator is defined as deleted unless
|
| 309 |
`is_copy_constructible_v<T>` is `true` and `is_copy_assignable_v<T>` is
|
| 310 |
+
`true`. If `is_trivially_copy_constructible_v<T> &&`
|
| 311 |
+
`is_trivially_copy_assignable_v<T> &&` `is_trivially_destructible_v<T>`
|
| 312 |
+
is `true`, this assignment operator is trivial.
|
| 313 |
|
| 314 |
``` cpp
|
| 315 |
+
constexpr optional& operator=(optional&& rhs) noexcept(see below);
|
| 316 |
```
|
| 317 |
|
| 318 |
+
*Constraints:* `is_move_constructible_v<T>` is `true` and
|
| 319 |
+
`is_move_assignable_v<T>` is `true`.
|
| 320 |
|
| 321 |
+
*Effects:* See [[optional.assign.move]]. The result of the expression
|
| 322 |
+
`bool(rhs)` remains unchanged.
|
| 323 |
+
|
| 324 |
+
**Table: `optional::operator=(optional&&)` effects** <a id="optional.assign.move">[optional.assign.move]</a>
|
| 325 |
|
| 326 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 327 |
| ------------------------------ | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
|
| 328 |
| `rhs` contains a value | assigns `std::move(*rhs)` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `std::move(*rhs)` |
|
| 329 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 330 |
|
| 331 |
|
| 332 |
+
*Ensures:* `bool(rhs) == bool(*this)`.
|
| 333 |
+
|
| 334 |
*Returns:* `*this`.
|
| 335 |
|
|
|
|
|
|
|
| 336 |
*Remarks:* The expression inside `noexcept` is equivalent to:
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
is_nothrow_move_assignable_v<T> && is_nothrow_move_constructible_v<T>
|
| 340 |
```
|
|
|
|
| 343 |
remains unchanged. If an exception is thrown during the call to `T`’s
|
| 344 |
move constructor, the state of `*rhs.val` is determined by the exception
|
| 345 |
safety guarantee of `T`’s move constructor. If an exception is thrown
|
| 346 |
during the call to `T`’s move assignment, the state of `*val` and
|
| 347 |
`*rhs.val` is determined by the exception safety guarantee of `T`’s move
|
| 348 |
+
assignment. If `is_trivially_move_constructible_v<T> &&`
|
| 349 |
+
`is_trivially_move_assignable_v<T> &&` `is_trivially_destructible_v<T>`
|
| 350 |
+
is `true`, this assignment operator is trivial.
|
| 351 |
|
| 352 |
``` cpp
|
| 353 |
template<class U = T> optional<T>& operator=(U&& v);
|
| 354 |
```
|
| 355 |
|
| 356 |
+
*Constraints:* `is_same_v<remove_cvref_t<U>, optional>` is `false`,
|
| 357 |
+
`conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>` is `false`,
|
| 358 |
+
`is_constructible_v<T, U>` is `true`, and `is_assignable_v<T&, U>` is
|
| 359 |
+
`true`.
|
| 360 |
+
|
| 361 |
*Effects:* If `*this` contains a value, assigns `std::forward<U>(v)` to
|
| 362 |
the contained value; otherwise initializes the contained value as if
|
| 363 |
direct-non-list-initializing object of type `T` with
|
| 364 |
`std::forward<U>(v)`.
|
| 365 |
|
| 366 |
+
*Ensures:* `*this` contains a value.
|
| 367 |
|
| 368 |
+
*Returns:* `*this`.
|
| 369 |
|
| 370 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 371 |
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 372 |
call to `T`’s constructor, the state of `v` is determined by the
|
| 373 |
exception safety guarantee of `T`’s constructor. If an exception is
|
| 374 |
thrown during the call to `T`’s assignment, the state of `*val` and `v`
|
| 375 |
is determined by the exception safety guarantee of `T`’s assignment.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 376 |
|
| 377 |
``` cpp
|
| 378 |
template<class U> optional<T>& operator=(const optional<U>& rhs);
|
| 379 |
```
|
| 380 |
|
| 381 |
+
*Constraints:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
|
| 383 |
- `is_constructible_v<T, const U&>` is `true`,
|
| 384 |
- `is_assignable_v<T&, const U&>` is `true`,
|
| 385 |
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 386 |
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
|
|
|
| 393 |
- `is_assignable_v<T&, optional<U>&>` is `false`,
|
| 394 |
- `is_assignable_v<T&, optional<U>&&>` is `false`,
|
| 395 |
- `is_assignable_v<T&, const optional<U>&>` is `false`, and
|
| 396 |
- `is_assignable_v<T&, const optional<U>&&>` is `false`.
|
| 397 |
|
| 398 |
+
*Effects:* See [[optional.assign.copy.templ]].
|
| 399 |
+
|
| 400 |
+
**Table: `optional::operator=(const optional<U>&)` effects** <a id="optional.assign.copy.templ">[optional.assign.copy.templ]</a>
|
| 401 |
+
|
| 402 |
+
| | `*this` contains a value | `*this` does not contain a value |
|
| 403 |
+
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------------------------------------- |
|
| 404 |
+
| `rhs` contains a value | assigns `*rhs` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `*rhs` |
|
| 405 |
+
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 406 |
+
|
| 407 |
+
|
| 408 |
+
*Ensures:* `bool(rhs) == bool(*this)`.
|
| 409 |
+
|
| 410 |
+
*Returns:* `*this`.
|
| 411 |
+
|
| 412 |
+
*Remarks:* If any exception is thrown, the result of the expression
|
| 413 |
+
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 414 |
+
call to `T`’s constructor, the state of `*rhs.val` is determined by the
|
| 415 |
+
exception safety guarantee of `T`’s constructor. If an exception is
|
| 416 |
+
thrown during the call to `T`’s assignment, the state of `*val` and
|
| 417 |
+
`*rhs.val` is determined by the exception safety guarantee of `T`’s
|
| 418 |
+
assignment.
|
| 419 |
+
|
| 420 |
``` cpp
|
| 421 |
template<class U> optional<T>& operator=(optional<U>&& rhs);
|
| 422 |
```
|
| 423 |
|
| 424 |
+
*Constraints:*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
|
| 426 |
- `is_constructible_v<T, U>` is `true`,
|
| 427 |
- `is_assignable_v<T&, U>` is `true`,
|
| 428 |
- `is_constructible_v<T, optional<U>&>` is `false`,
|
| 429 |
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
|
|
|
| 436 |
- `is_assignable_v<T&, optional<U>&>` is `false`,
|
| 437 |
- `is_assignable_v<T&, optional<U>&&>` is `false`,
|
| 438 |
- `is_assignable_v<T&, const optional<U>&>` is `false`, and
|
| 439 |
- `is_assignable_v<T&, const optional<U>&&>` is `false`.
|
| 440 |
|
| 441 |
+
*Effects:* See [[optional.assign.move.templ]]. The result of the
|
| 442 |
+
expression `bool(rhs)` remains unchanged.
|
| 443 |
+
|
| 444 |
+
**Table: `optional::operator=(optional<U>&&)` effects** <a id="optional.assign.move.templ">[optional.assign.move.templ]</a>
|
| 445 |
+
|
| 446 |
+
| | `*this` contains a value | `*this` does not contain a value |
|
| 447 |
+
| ------------------------------ | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------- |
|
| 448 |
+
| `rhs` contains a value | assigns `std::move(*rhs)` to the contained value | initializes the contained value as if direct-non-list-initializing an object of type `T` with `std::move(*rhs)` |
|
| 449 |
+
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 450 |
+
|
| 451 |
+
|
| 452 |
+
*Ensures:* `bool(rhs) == bool(*this)`.
|
| 453 |
+
|
| 454 |
+
*Returns:* `*this`.
|
| 455 |
+
|
| 456 |
+
*Remarks:* If any exception is thrown, the result of the expression
|
| 457 |
+
`bool(*this)` remains unchanged. If an exception is thrown during the
|
| 458 |
+
call to `T`’s constructor, the state of `*rhs.val` is determined by the
|
| 459 |
+
exception safety guarantee of `T`’s constructor. If an exception is
|
| 460 |
+
thrown during the call to `T`’s assignment, the state of `*val` and
|
| 461 |
+
`*rhs.val` is determined by the exception safety guarantee of `T`’s
|
| 462 |
+
assignment.
|
| 463 |
+
|
| 464 |
``` cpp
|
| 465 |
template<class... Args> T& emplace(Args&&... args);
|
| 466 |
```
|
| 467 |
|
| 468 |
+
*Mandates:* `is_constructible_v<T, Args...>` is `true`.
|
| 469 |
|
| 470 |
*Effects:* Calls `*this = nullopt`. Then initializes the contained value
|
| 471 |
as if direct-non-list-initializing an object of type `T` with the
|
| 472 |
arguments `std::forward<Args>(args)...`.
|
| 473 |
|
| 474 |
+
*Ensures:* `*this` contains a value.
|
| 475 |
|
| 476 |
*Returns:* A reference to the new contained value.
|
| 477 |
|
| 478 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 479 |
|
|
|
|
| 483 |
|
| 484 |
``` cpp
|
| 485 |
template<class U, class... Args> T& emplace(initializer_list<U> il, Args&&... args);
|
| 486 |
```
|
| 487 |
|
| 488 |
+
*Constraints:* `is_constructible_v<T, initializer_list<U>&, Args...>` is
|
| 489 |
+
`true`.
|
| 490 |
+
|
| 491 |
*Effects:* Calls `*this = nullopt`. Then initializes the contained value
|
| 492 |
as if direct-non-list-initializing an object of type `T` with the
|
| 493 |
arguments `il, std::forward<Args>(args)...`.
|
| 494 |
|
| 495 |
+
*Ensures:* `*this` contains a value.
|
| 496 |
|
| 497 |
*Returns:* A reference to the new contained value.
|
| 498 |
|
| 499 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 500 |
|
| 501 |
*Remarks:* If an exception is thrown during the call to `T`’s
|
| 502 |
constructor, `*this` does not contain a value, and the previous `*val`
|
| 503 |
+
(if any) has been destroyed.
|
|
|
|
|
|
|
| 504 |
|
| 505 |
#### Swap <a id="optional.swap">[[optional.swap]]</a>
|
| 506 |
|
| 507 |
``` cpp
|
| 508 |
void swap(optional& rhs) noexcept(see below);
|
| 509 |
```
|
| 510 |
|
| 511 |
+
*Mandates:* `is_move_constructible_v<T>` is `true`.
|
|
|
|
| 512 |
|
| 513 |
+
*Preconditions:* Lvalues of type `T` are swappable.
|
| 514 |
|
| 515 |
+
*Effects:* See [[optional.swap]].
|
| 516 |
+
|
| 517 |
+
**Table: `optional::swap(optional&)` effects** <a id="optional.swap">[optional.swap]</a>
|
| 518 |
|
| 519 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 520 |
| ------------------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
| 521 |
| `rhs` contains a value | calls `swap(*(*this), *rhs)` | initializes the contained value of `*this` as if direct-non-list-initializing an object of type `T` with the expression `std::move(*rhs)`, followed by `rhs.val->T::~T()`; postcondition is that `*this` contains a value and `rhs` does not contain a value |
|
| 522 |
| `rhs` does not contain a value | initializes the contained value of `rhs` as if direct-non-list-initializing an object of type `T` with the expression `std::move(*(*this))`, followed by `val->T::~T()`; postcondition is that `*this` does not contain a value and `rhs` contains a value | no effect |
|
| 523 |
|
| 524 |
|
| 525 |
*Throws:* Any exceptions thrown by the operations in the relevant part
|
| 526 |
+
of [[optional.swap]].
|
| 527 |
|
| 528 |
*Remarks:* The expression inside `noexcept` is equivalent to:
|
| 529 |
|
| 530 |
``` cpp
|
| 531 |
is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T>
|
|
|
|
| 544 |
``` cpp
|
| 545 |
constexpr const T* operator->() const;
|
| 546 |
constexpr T* operator->();
|
| 547 |
```
|
| 548 |
|
| 549 |
+
*Preconditions:* `*this` contains a value.
|
| 550 |
|
| 551 |
*Returns:* `val`.
|
| 552 |
|
| 553 |
*Throws:* Nothing.
|
| 554 |
|
| 555 |
+
*Remarks:* These functions are constexpr functions.
|
| 556 |
|
| 557 |
``` cpp
|
| 558 |
constexpr const T& operator*() const&;
|
| 559 |
constexpr T& operator*() &;
|
| 560 |
```
|
| 561 |
|
| 562 |
+
*Preconditions:* `*this` contains a value.
|
| 563 |
|
| 564 |
*Returns:* `*val`.
|
| 565 |
|
| 566 |
*Throws:* Nothing.
|
| 567 |
|
| 568 |
+
*Remarks:* These functions are constexpr functions.
|
| 569 |
|
| 570 |
``` cpp
|
| 571 |
constexpr T&& operator*() &&;
|
| 572 |
constexpr const T&& operator*() const&&;
|
| 573 |
```
|
| 574 |
|
| 575 |
+
*Preconditions:* `*this` contains a value.
|
| 576 |
|
| 577 |
*Effects:* Equivalent to: `return std::move(*val);`
|
| 578 |
|
| 579 |
``` cpp
|
| 580 |
constexpr explicit operator bool() const noexcept;
|
| 581 |
```
|
| 582 |
|
| 583 |
*Returns:* `true` if and only if `*this` contains a value.
|
| 584 |
|
| 585 |
+
*Remarks:* This function is a constexpr function.
|
| 586 |
|
| 587 |
``` cpp
|
| 588 |
constexpr bool has_value() const noexcept;
|
| 589 |
```
|
| 590 |
|
| 591 |
*Returns:* `true` if and only if `*this` contains a value.
|
| 592 |
|
| 593 |
+
*Remarks:* This function is a constexpr function.
|
| 594 |
|
| 595 |
``` cpp
|
| 596 |
constexpr const T& value() const&;
|
| 597 |
constexpr T& value() &;
|
| 598 |
```
|
|
|
|
| 616 |
|
| 617 |
``` cpp
|
| 618 |
template<class U> constexpr T value_or(U&& v) const&;
|
| 619 |
```
|
| 620 |
|
| 621 |
+
*Mandates:* `is_copy_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 622 |
+
`true`.
|
| 623 |
+
|
| 624 |
*Effects:* Equivalent to:
|
| 625 |
|
| 626 |
``` cpp
|
| 627 |
return bool(*this) ? **this : static_cast<T>(std::forward<U>(v));
|
| 628 |
```
|
| 629 |
|
|
|
|
|
|
|
|
|
|
| 630 |
``` cpp
|
| 631 |
template<class U> constexpr T value_or(U&& v) &&;
|
| 632 |
```
|
| 633 |
|
| 634 |
+
*Mandates:* `is_move_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 635 |
+
`true`.
|
| 636 |
+
|
| 637 |
*Effects:* Equivalent to:
|
| 638 |
|
| 639 |
``` cpp
|
| 640 |
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
| 641 |
```
|
| 642 |
|
|
|
|
|
|
|
|
|
|
| 643 |
#### Modifiers <a id="optional.mod">[[optional.mod]]</a>
|
| 644 |
|
| 645 |
``` cpp
|
| 646 |
void reset() noexcept;
|
| 647 |
```
|
| 648 |
|
| 649 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 650 |
the contained value; otherwise no effect.
|
| 651 |
|
| 652 |
+
*Ensures:* `*this` does not contain a value.
|
| 653 |
|