- tmp/tmpovgtfdhh/{from.md → to.md} +281 -163
tmp/tmpovgtfdhh/{from.md → to.md}
RENAMED
|
@@ -1,7 +1,9 @@
|
|
| 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:
|
|
@@ -17,48 +19,60 @@ namespace std {
|
|
| 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&&...);
|
| 38 |
|
| 39 |
// [optional.swap], swap
|
| 40 |
-
void swap(optional&) noexcept(see below);
|
| 41 |
|
| 42 |
// [optional.observe], observers
|
| 43 |
-
constexpr const T* operator->() const;
|
| 44 |
-
constexpr T* operator->();
|
| 45 |
-
constexpr const T& operator*() const&;
|
| 46 |
-
constexpr T& operator*() &;
|
| 47 |
-
constexpr T&& operator*() &&;
|
| 48 |
-
constexpr const T&& operator*() const&&;
|
| 49 |
constexpr explicit operator bool() const noexcept;
|
| 50 |
constexpr bool has_value() const noexcept;
|
| 51 |
constexpr const T& value() const &;
|
| 52 |
constexpr T& value() &;
|
| 53 |
constexpr T&& value() &&;
|
| 54 |
constexpr const T&& value() const &&;
|
| 55 |
template<class U> constexpr T value_or(U&&) const &;
|
| 56 |
template<class U> constexpr T value_or(U&&) &&;
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
// [optional.mod], modifiers
|
| 59 |
-
void reset() noexcept;
|
| 60 |
|
| 61 |
private:
|
| 62 |
T *val; // exposition only
|
| 63 |
};
|
| 64 |
|
|
@@ -70,25 +84,35 @@ namespace std {
|
|
| 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
|
| 73 |
optional object’s *contained value*, is allocated within the storage of
|
| 74 |
the optional object. Implementations are not permitted to use additional
|
| 75 |
-
storage, such as dynamic memory, to allocate its contained value.
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
returns `true` if the object contains a value; otherwise the conversion
|
| 80 |
-
returns `false`.
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 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 |
|
|
@@ -99,15 +123,14 @@ 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
|
| 105 |
-
|
| 106 |
-
expression `*rhs`.
|
| 107 |
|
| 108 |
-
*Ensures:* `
|
| 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
|
|
@@ -118,31 +141,29 @@ trivial.
|
|
| 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
|
| 124 |
-
|
| 125 |
-
expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
| 126 |
|
| 127 |
-
*Ensures:* `
|
| 128 |
|
| 129 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 130 |
|
| 131 |
-
*Remarks:* The
|
| 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:*
|
| 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`.
|
|
@@ -156,12 +177,11 @@ template<class U, class... Args>
|
|
| 156 |
```
|
| 157 |
|
| 158 |
*Constraints:* `is_constructible_v<T, initializer_list<U>&, Args...>` is
|
| 159 |
`true`.
|
| 160 |
|
| 161 |
-
*Effects:*
|
| 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`.
|
|
@@ -171,16 +191,19 @@ 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:*
|
| 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 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
`std::forward<U>(v)`.
|
| 183 |
|
| 184 |
*Ensures:* `*this` contains a value.
|
| 185 |
|
| 186 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
|
@@ -192,60 +215,46 @@ this constructor is a constexpr constructor. The expression inside
|
|
| 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 |
-
- `
|
| 204 |
-
|
| 205 |
-
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
| 206 |
-
- `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
|
| 213 |
-
|
| 214 |
-
expression `*rhs`.
|
| 215 |
|
| 216 |
-
*Ensures:* `
|
| 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 |
-
- `
|
| 234 |
-
|
| 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
|
| 243 |
-
|
| 244 |
-
expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
| 245 |
|
| 246 |
-
*Ensures:* `
|
| 247 |
|
| 248 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 249 |
|
| 250 |
*Remarks:* The expression inside `explicit` is equivalent to:
|
| 251 |
|
|
@@ -254,11 +263,11 @@ expression `std::move(*rhs)`. `bool(rhs)` is unchanged.
|
|
| 254 |
```
|
| 255 |
|
| 256 |
#### Destructor <a id="optional.dtor">[[optional.dtor]]</a>
|
| 257 |
|
| 258 |
``` cpp
|
| 259 |
-
~optional();
|
| 260 |
```
|
| 261 |
|
| 262 |
*Effects:* If `is_trivially_destructible_v<T> != true` and `*this`
|
| 263 |
contains a value, calls
|
| 264 |
|
|
@@ -270,11 +279,11 @@ val->T::~T()
|
|
| 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 |
|
|
@@ -289,22 +298,22 @@ constexpr optional<T>& operator=(const optional& rhs);
|
|
| 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 |
|
| 296 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 297 |
|
| 298 |
|
| 299 |
-
*Ensures:* `
|
| 300 |
|
| 301 |
*Returns:* `*this`.
|
| 302 |
|
| 303 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 304 |
-
`
|
| 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> &&`
|
|
@@ -317,161 +326,146 @@ constexpr optional& operator=(optional&& rhs) noexcept(see below);
|
|
| 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 |
-
`
|
| 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 |
|
| 329 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 330 |
|
| 331 |
|
| 332 |
-
*Ensures:* `
|
| 333 |
|
| 334 |
*Returns:* `*this`.
|
| 335 |
|
| 336 |
-
*Remarks:* The
|
| 337 |
|
| 338 |
``` cpp
|
| 339 |
is_nothrow_move_assignable_v<T> && is_nothrow_move_constructible_v<T>
|
| 340 |
```
|
| 341 |
|
| 342 |
-
If any exception is thrown, the result of the expression
|
| 343 |
-
remains unchanged. If an exception is thrown during
|
| 344 |
-
move constructor, the state of `*rhs.val` is
|
| 345 |
-
safety guarantee of `T`’s move constructor.
|
| 346 |
-
during the call to `T`’s move assignment, the
|
| 347 |
-
`*rhs.val` is determined by the exception safety
|
| 348 |
-
|
|
|
|
| 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
|
| 363 |
-
|
| 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 |
-
`
|
| 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 |
-
- `
|
| 386 |
-
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
| 387 |
-
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
| 388 |
-
- `is_constructible_v<T, const optional<U>&&>` is `false`,
|
| 389 |
-
- `is_convertible_v<optional<U>&, T>` is `false`,
|
| 390 |
-
- `is_convertible_v<optional<U>&&, T>` is `false`,
|
| 391 |
-
- `is_convertible_v<const optional<U>&, T>` is `false`,
|
| 392 |
-
- `is_convertible_v<const optional<U>&&, T>` 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 |
|
| 405 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 406 |
|
| 407 |
|
| 408 |
-
*Ensures:* `
|
| 409 |
|
| 410 |
*Returns:* `*this`.
|
| 411 |
|
| 412 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 413 |
-
`
|
| 414 |
-
call to `T`’s constructor, the state of `*rhs.val` is determined by
|
| 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 |
-
- `
|
| 429 |
-
- `is_constructible_v<T, optional<U>&&>` is `false`,
|
| 430 |
-
- `is_constructible_v<T, const optional<U>&>` is `false`,
|
| 431 |
-
- `is_constructible_v<T, const optional<U>&&>` is `false`,
|
| 432 |
-
- `is_convertible_v<optional<U>&, T>` is `false`,
|
| 433 |
-
- `is_convertible_v<optional<U>&&, T>` is `false`,
|
| 434 |
-
- `is_convertible_v<const optional<U>&, T>` is `false`,
|
| 435 |
-
- `is_convertible_v<const optional<U>&&, T>` 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 `
|
| 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 |
|
| 449 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 450 |
|
| 451 |
|
| 452 |
-
*Ensures:* `
|
| 453 |
|
| 454 |
*Returns:* `*this`.
|
| 455 |
|
| 456 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 457 |
-
`
|
| 458 |
-
call to `T`’s constructor, the state of `*rhs.val` is determined by
|
| 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
|
| 471 |
-
|
| 472 |
-
arguments `std::forward<Args>(args)...`.
|
| 473 |
|
| 474 |
*Ensures:* `*this` contains a value.
|
| 475 |
|
| 476 |
*Returns:* A reference to the new contained value.
|
| 477 |
|
|
@@ -480,19 +474,18 @@ arguments `std::forward<Args>(args)...`.
|
|
| 480 |
*Remarks:* If an exception is thrown during the call to `T`’s
|
| 481 |
constructor, `*this` does not contain a value, and the previous `*val`
|
| 482 |
(if any) has been destroyed.
|
| 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
|
| 492 |
-
|
| 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 |
|
|
@@ -503,75 +496,72 @@ 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:*
|
|
|
|
| 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)`
|
| 522 |
-
| `rhs` does not contain a value | initializes the contained value of `rhs`
|
| 523 |
|
| 524 |
|
| 525 |
*Throws:* Any exceptions thrown by the operations in the relevant part
|
| 526 |
of [[optional.swap]].
|
| 527 |
|
| 528 |
-
*Remarks:* The
|
| 529 |
|
| 530 |
``` cpp
|
| 531 |
is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T>
|
| 532 |
```
|
| 533 |
|
| 534 |
-
If any exception is thrown, the results of the expressions
|
| 535 |
-
and `
|
| 536 |
-
call to function `swap`, the state of
|
| 537 |
-
determined by the exception safety guarantee of
|
| 538 |
-
`T`. If an exception is thrown during the call to
|
| 539 |
-
constructor, the state of `*val` and `*rhs.val` is determined
|
| 540 |
-
exception safety guarantee of `T`’s move constructor.
|
| 541 |
|
| 542 |
#### Observers <a id="optional.observe">[[optional.observe]]</a>
|
| 543 |
|
| 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);`
|
|
@@ -598,22 +588,22 @@ constexpr T& value() &;
|
|
| 598 |
```
|
| 599 |
|
| 600 |
*Effects:* Equivalent to:
|
| 601 |
|
| 602 |
``` cpp
|
| 603 |
-
return
|
| 604 |
```
|
| 605 |
|
| 606 |
``` cpp
|
| 607 |
constexpr T&& value() &&;
|
| 608 |
constexpr const T&& value() const &&;
|
| 609 |
```
|
| 610 |
|
| 611 |
*Effects:* Equivalent to:
|
| 612 |
|
| 613 |
``` cpp
|
| 614 |
-
return
|
| 615 |
```
|
| 616 |
|
| 617 |
``` cpp
|
| 618 |
template<class U> constexpr T value_or(U&& v) const &;
|
| 619 |
```
|
|
@@ -622,11 +612,11 @@ template<class U> constexpr T value_or(U&& v) const&;
|
|
| 622 |
`true`.
|
| 623 |
|
| 624 |
*Effects:* Equivalent to:
|
| 625 |
|
| 626 |
``` cpp
|
| 627 |
-
return
|
| 628 |
```
|
| 629 |
|
| 630 |
``` cpp
|
| 631 |
template<class U> constexpr T value_or(U&& v) &&;
|
| 632 |
```
|
|
@@ -635,17 +625,145 @@ template<class U> constexpr T value_or(U&& v) &&;
|
|
| 635 |
`true`.
|
| 636 |
|
| 637 |
*Effects:* Equivalent to:
|
| 638 |
|
| 639 |
``` cpp
|
| 640 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
|
|
|
|
| 1 |
### Class template `optional` <a id="optional.optional">[[optional.optional]]</a>
|
| 2 |
|
| 3 |
+
#### General <a id="optional.optional.general">[[optional.optional.general]]</a>
|
| 4 |
+
|
| 5 |
``` cpp
|
| 6 |
namespace std {
|
| 7 |
template<class T>
|
| 8 |
class optional {
|
| 9 |
public:
|
|
|
|
| 19 |
template<class U, class... Args>
|
| 20 |
constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
|
| 21 |
template<class U = T>
|
| 22 |
constexpr explicit(see below) optional(U&&);
|
| 23 |
template<class U>
|
| 24 |
+
constexpr explicit(see below) optional(const optional<U>&);
|
| 25 |
template<class U>
|
| 26 |
+
constexpr explicit(see below) optional(optional<U>&&);
|
| 27 |
|
| 28 |
// [optional.dtor], destructor
|
| 29 |
+
constexpr ~optional();
|
| 30 |
|
| 31 |
// [optional.assign], assignment
|
| 32 |
+
constexpr optional& operator=(nullopt_t) noexcept;
|
| 33 |
constexpr optional& operator=(const optional&);
|
| 34 |
constexpr optional& operator=(optional&&) noexcept(see below);
|
| 35 |
+
template<class U = T> constexpr optional& operator=(U&&);
|
| 36 |
+
template<class U> constexpr optional& operator=(const optional<U>&);
|
| 37 |
+
template<class U> constexpr optional& operator=(optional<U>&&);
|
| 38 |
+
template<class... Args> constexpr T& emplace(Args&&...);
|
| 39 |
+
template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...);
|
| 40 |
|
| 41 |
// [optional.swap], swap
|
| 42 |
+
constexpr void swap(optional&) noexcept(see below);
|
| 43 |
|
| 44 |
// [optional.observe], observers
|
| 45 |
+
constexpr const T* operator->() const noexcept;
|
| 46 |
+
constexpr T* operator->() noexcept;
|
| 47 |
+
constexpr const T& operator*() const & noexcept;
|
| 48 |
+
constexpr T& operator*() & noexcept;
|
| 49 |
+
constexpr T&& operator*() && noexcept;
|
| 50 |
+
constexpr const T&& operator*() const && noexcept;
|
| 51 |
constexpr explicit operator bool() const noexcept;
|
| 52 |
constexpr bool has_value() const noexcept;
|
| 53 |
constexpr const T& value() const &;
|
| 54 |
constexpr T& value() &;
|
| 55 |
constexpr T&& value() &&;
|
| 56 |
constexpr const T&& value() const &&;
|
| 57 |
template<class U> constexpr T value_or(U&&) const &;
|
| 58 |
template<class U> constexpr T value_or(U&&) &&;
|
| 59 |
|
| 60 |
+
// [optional.monadic], monadic operations
|
| 61 |
+
template<class F> constexpr auto and_then(F&& f) &;
|
| 62 |
+
template<class F> constexpr auto and_then(F&& f) &&;
|
| 63 |
+
template<class F> constexpr auto and_then(F&& f) const &;
|
| 64 |
+
template<class F> constexpr auto and_then(F&& f) const &&;
|
| 65 |
+
template<class F> constexpr auto transform(F&& f) &;
|
| 66 |
+
template<class F> constexpr auto transform(F&& f) &&;
|
| 67 |
+
template<class F> constexpr auto transform(F&& f) const &;
|
| 68 |
+
template<class F> constexpr auto transform(F&& f) const &&;
|
| 69 |
+
template<class F> constexpr optional or_else(F&& f) &&;
|
| 70 |
+
template<class F> constexpr optional or_else(F&& f) const &;
|
| 71 |
+
|
| 72 |
// [optional.mod], modifiers
|
| 73 |
+
constexpr void reset() noexcept;
|
| 74 |
|
| 75 |
private:
|
| 76 |
T *val; // exposition only
|
| 77 |
};
|
| 78 |
|
|
|
|
| 84 |
Any instance of `optional<T>` at any given time either contains a value
|
| 85 |
or does not contain a value. When an instance of `optional<T>` *contains
|
| 86 |
a value*, it means that an object of type `T`, referred to as the
|
| 87 |
optional object’s *contained value*, is allocated within the storage of
|
| 88 |
the optional object. Implementations are not permitted to use additional
|
| 89 |
+
storage, such as dynamic memory, to allocate its contained value. When
|
| 90 |
+
an object of type `optional<T>` is contextually converted to `bool`, the
|
| 91 |
+
conversion returns `true` if the object contains a value; otherwise the
|
| 92 |
+
conversion returns `false`.
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
When an `optional<T>` object contains a value, member `val` points to
|
| 95 |
+
the contained value.
|
| 96 |
|
| 97 |
`T` shall be a type other than cv `in_place_t` or cv `nullopt_t` that
|
| 98 |
meets the *Cpp17Destructible* requirements ([[cpp17.destructible]]).
|
| 99 |
|
| 100 |
#### Constructors <a id="optional.ctor">[[optional.ctor]]</a>
|
| 101 |
|
| 102 |
+
The exposition-only variable template *`converts-from-any-cvref`* is
|
| 103 |
+
used by some constructors for `optional`.
|
| 104 |
+
|
| 105 |
+
``` cpp
|
| 106 |
+
template<class T, class W>
|
| 107 |
+
constexpr bool converts-from-any-cvref = // exposition only
|
| 108 |
+
disjunction_v<is_constructible<T, W&>, is_convertible<W&, T>,
|
| 109 |
+
is_constructible<T, W>, is_convertible<W, T>,
|
| 110 |
+
is_constructible<T, const W&>, is_convertible<const W&, T>,
|
| 111 |
+
is_constructible<T, const W>, is_convertible<const W, T>>;
|
| 112 |
+
```
|
| 113 |
+
|
| 114 |
``` cpp
|
| 115 |
constexpr optional() noexcept;
|
| 116 |
constexpr optional(nullopt_t) noexcept;
|
| 117 |
```
|
| 118 |
|
|
|
|
| 123 |
|
| 124 |
``` cpp
|
| 125 |
constexpr optional(const optional& rhs);
|
| 126 |
```
|
| 127 |
|
| 128 |
+
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 129 |
+
contained value with `*rhs`.
|
|
|
|
| 130 |
|
| 131 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 132 |
|
| 133 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 134 |
|
| 135 |
*Remarks:* This constructor is defined as deleted unless
|
| 136 |
`is_copy_constructible_v<T>` is `true`. If
|
|
|
|
| 141 |
constexpr optional(optional&& rhs) noexcept(see below);
|
| 142 |
```
|
| 143 |
|
| 144 |
*Constraints:* `is_move_constructible_v<T>` is `true`.
|
| 145 |
|
| 146 |
+
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 147 |
+
contained value with `std::move(*rhs)`. `rhs.has_value()` is unchanged.
|
|
|
|
| 148 |
|
| 149 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 150 |
|
| 151 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 152 |
|
| 153 |
+
*Remarks:* The exception specification is equivalent to
|
| 154 |
`is_nothrow_move_constructible_v<T>`. If
|
| 155 |
`is_trivially_move_constructible_v<T>` is `true`, this constructor is
|
| 156 |
trivial.
|
| 157 |
|
| 158 |
``` cpp
|
| 159 |
template<class... Args> constexpr explicit optional(in_place_t, Args&&... args);
|
| 160 |
```
|
| 161 |
|
| 162 |
*Constraints:* `is_constructible_v<T, Args...>` is `true`.
|
| 163 |
|
| 164 |
+
*Effects:* Direct-non-list-initializes the contained value with
|
|
|
|
| 165 |
`std::forward<Args>(args)...`.
|
| 166 |
|
| 167 |
*Ensures:* `*this` contains a value.
|
| 168 |
|
| 169 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
|
|
|
| 177 |
```
|
| 178 |
|
| 179 |
*Constraints:* `is_constructible_v<T, initializer_list<U>&, Args...>` is
|
| 180 |
`true`.
|
| 181 |
|
| 182 |
+
*Effects:* Direct-non-list-initializes the contained value with
|
|
|
|
| 183 |
`il, std::forward<Args>(args)...`.
|
| 184 |
|
| 185 |
*Ensures:* `*this` contains a value.
|
| 186 |
|
| 187 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
|
|
|
| 191 |
|
| 192 |
``` cpp
|
| 193 |
template<class U = T> constexpr explicit(see below) optional(U&& v);
|
| 194 |
```
|
| 195 |
|
| 196 |
+
*Constraints:*
|
|
|
|
|
|
|
| 197 |
|
| 198 |
+
- `is_constructible_v<T, U>` is `true`,
|
| 199 |
+
- `is_same_v<remove_cvref_t<U>, in_place_t>` is `false`,
|
| 200 |
+
- `is_same_v<remove_cvref_t<U>, optional>` is `false`, and
|
| 201 |
+
- if `T` is cv `bool`, `remove_cvref_t<U>` is not a specialization of
|
| 202 |
+
`optional`.
|
| 203 |
+
|
| 204 |
+
*Effects:* Direct-non-list-initializes the contained value with
|
| 205 |
`std::forward<U>(v)`.
|
| 206 |
|
| 207 |
*Ensures:* `*this` contains a value.
|
| 208 |
|
| 209 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
|
|
|
| 215 |
``` cpp
|
| 216 |
!is_convertible_v<U, T>
|
| 217 |
```
|
| 218 |
|
| 219 |
``` cpp
|
| 220 |
+
template<class U> constexpr explicit(see below) optional(const optional<U>& rhs);
|
| 221 |
```
|
| 222 |
|
| 223 |
*Constraints:*
|
| 224 |
|
| 225 |
+
- `is_constructible_v<T, const U&>` is `true`, and
|
| 226 |
+
- if `T` is not cv `bool`, *`converts-from-any-cvref`*`<T, optional<U>>`
|
| 227 |
+
is `false`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
+
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 230 |
+
contained value with `*rhs`.
|
|
|
|
| 231 |
|
| 232 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 233 |
|
| 234 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 235 |
|
| 236 |
*Remarks:* The expression inside `explicit` is equivalent to:
|
| 237 |
|
| 238 |
``` cpp
|
| 239 |
!is_convertible_v<const U&, T>
|
| 240 |
```
|
| 241 |
|
| 242 |
``` cpp
|
| 243 |
+
template<class U> constexpr explicit(see below) optional(optional<U>&& rhs);
|
| 244 |
```
|
| 245 |
|
| 246 |
*Constraints:*
|
| 247 |
|
| 248 |
+
- `is_constructible_v<T, U>` is `true`, and
|
| 249 |
+
- if `T` is not cv `bool`, *`converts-from-any-cvref`*`<T, optional<U>>`
|
| 250 |
+
is `false`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
|
| 252 |
+
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 253 |
+
contained value with `std::move(*rhs)`. `rhs.has_value()` is unchanged.
|
|
|
|
| 254 |
|
| 255 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 256 |
|
| 257 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 258 |
|
| 259 |
*Remarks:* The expression inside `explicit` is equivalent to:
|
| 260 |
|
|
|
|
| 263 |
```
|
| 264 |
|
| 265 |
#### Destructor <a id="optional.dtor">[[optional.dtor]]</a>
|
| 266 |
|
| 267 |
``` cpp
|
| 268 |
+
constexpr ~optional();
|
| 269 |
```
|
| 270 |
|
| 271 |
*Effects:* If `is_trivially_destructible_v<T> != true` and `*this`
|
| 272 |
contains a value, calls
|
| 273 |
|
|
|
|
| 279 |
destructor is trivial.
|
| 280 |
|
| 281 |
#### Assignment <a id="optional.assign">[[optional.assign]]</a>
|
| 282 |
|
| 283 |
``` cpp
|
| 284 |
+
constexpr optional<T>& operator=(nullopt_t) noexcept;
|
| 285 |
```
|
| 286 |
|
| 287 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 288 |
the contained value; otherwise no effect.
|
| 289 |
|
|
|
|
| 298 |
*Effects:* See [[optional.assign.copy]].
|
| 299 |
|
| 300 |
**Table: `optional::operator=(const optional&)` effects** <a id="optional.assign.copy">[optional.assign.copy]</a>
|
| 301 |
|
| 302 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 303 |
+
| ------------------------------ | ------------------------------------------------------ | ----------------------------------------------------------- |
|
| 304 |
+
| `rhs` contains a value | assigns `*rhs` to the contained value | direct-non-list-initializes the contained value with `*rhs` |
|
| 305 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 306 |
|
| 307 |
|
| 308 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 309 |
|
| 310 |
*Returns:* `*this`.
|
| 311 |
|
| 312 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 313 |
+
`this->has_value()` remains unchanged. If an exception is thrown during
|
| 314 |
+
the call to `T`’s copy constructor, no effect. If an exception is thrown
|
| 315 |
during the call to `T`’s copy assignment, the state of its contained
|
| 316 |
value is as defined by the exception safety guarantee of `T`’s copy
|
| 317 |
assignment. This operator is defined as deleted unless
|
| 318 |
`is_copy_constructible_v<T>` is `true` and `is_copy_assignable_v<T>` is
|
| 319 |
`true`. If `is_trivially_copy_constructible_v<T> &&`
|
|
|
|
| 326 |
|
| 327 |
*Constraints:* `is_move_constructible_v<T>` is `true` and
|
| 328 |
`is_move_assignable_v<T>` is `true`.
|
| 329 |
|
| 330 |
*Effects:* See [[optional.assign.move]]. The result of the expression
|
| 331 |
+
`rhs.has_value()` remains unchanged.
|
| 332 |
|
| 333 |
**Table: `optional::operator=(optional&&)` effects** <a id="optional.assign.move">[optional.assign.move]</a>
|
| 334 |
|
| 335 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 336 |
+
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 337 |
+
| `rhs` contains a value | assigns `std::move(*rhs)` to the contained value | direct-non-list-initializes the contained value with `std::move(*rhs)` |
|
| 338 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 339 |
|
| 340 |
|
| 341 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 342 |
|
| 343 |
*Returns:* `*this`.
|
| 344 |
|
| 345 |
+
*Remarks:* The exception specification is equivalent to:
|
| 346 |
|
| 347 |
``` cpp
|
| 348 |
is_nothrow_move_assignable_v<T> && is_nothrow_move_constructible_v<T>
|
| 349 |
```
|
| 350 |
|
| 351 |
+
If any exception is thrown, the result of the expression
|
| 352 |
+
`this->has_value()` remains unchanged. If an exception is thrown during
|
| 353 |
+
the call to `T`’s move constructor, the state of `*rhs.val` is
|
| 354 |
+
determined by the exception safety guarantee of `T`’s move constructor.
|
| 355 |
+
If an exception is thrown during the call to `T`’s move assignment, the
|
| 356 |
+
state of `*val` and `*rhs.val` is determined by the exception safety
|
| 357 |
+
guarantee of `T`’s move assignment. If
|
| 358 |
+
`is_trivially_move_constructible_v<T> &&`
|
| 359 |
`is_trivially_move_assignable_v<T> &&` `is_trivially_destructible_v<T>`
|
| 360 |
is `true`, this assignment operator is trivial.
|
| 361 |
|
| 362 |
``` cpp
|
| 363 |
+
template<class U = T> constexpr optional<T>& operator=(U&& v);
|
| 364 |
```
|
| 365 |
|
| 366 |
*Constraints:* `is_same_v<remove_cvref_t<U>, optional>` is `false`,
|
| 367 |
`conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>` is `false`,
|
| 368 |
`is_constructible_v<T, U>` is `true`, and `is_assignable_v<T&, U>` is
|
| 369 |
`true`.
|
| 370 |
|
| 371 |
*Effects:* If `*this` contains a value, assigns `std::forward<U>(v)` to
|
| 372 |
+
the contained value; otherwise direct-non-list-initializes the contained
|
| 373 |
+
value with `std::forward<U>(v)`.
|
|
|
|
| 374 |
|
| 375 |
*Ensures:* `*this` contains a value.
|
| 376 |
|
| 377 |
*Returns:* `*this`.
|
| 378 |
|
| 379 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 380 |
+
`this->has_value()` remains unchanged. If an exception is thrown during
|
| 381 |
+
the call to `T`’s constructor, the state of `v` is determined by the
|
| 382 |
exception safety guarantee of `T`’s constructor. If an exception is
|
| 383 |
thrown during the call to `T`’s assignment, the state of `*val` and `v`
|
| 384 |
is determined by the exception safety guarantee of `T`’s assignment.
|
| 385 |
|
| 386 |
``` cpp
|
| 387 |
+
template<class U> constexpr optional<T>& operator=(const optional<U>& rhs);
|
| 388 |
```
|
| 389 |
|
| 390 |
*Constraints:*
|
| 391 |
|
| 392 |
- `is_constructible_v<T, const U&>` is `true`,
|
| 393 |
- `is_assignable_v<T&, const U&>` is `true`,
|
| 394 |
+
- *`converts-from-any-cvref`*`<T, optional<U>>` is `false`,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 395 |
- `is_assignable_v<T&, optional<U>&>` is `false`,
|
| 396 |
- `is_assignable_v<T&, optional<U>&&>` is `false`,
|
| 397 |
- `is_assignable_v<T&, const optional<U>&>` is `false`, and
|
| 398 |
- `is_assignable_v<T&, const optional<U>&&>` is `false`.
|
| 399 |
|
| 400 |
*Effects:* See [[optional.assign.copy.templ]].
|
| 401 |
|
| 402 |
**Table: `optional::operator=(const optional<U>&)` effects** <a id="optional.assign.copy.templ">[optional.assign.copy.templ]</a>
|
| 403 |
|
| 404 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 405 |
+
| ------------------------------ | ------------------------------------------------------ | ----------------------------------------------------------- |
|
| 406 |
+
| `rhs` contains a value | assigns `*rhs` to the contained value | direct-non-list-initializes the contained value with `*rhs` |
|
| 407 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 408 |
|
| 409 |
|
| 410 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 411 |
|
| 412 |
*Returns:* `*this`.
|
| 413 |
|
| 414 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 415 |
+
`this->has_value()` remains unchanged. If an exception is thrown during
|
| 416 |
+
the call to `T`’s constructor, the state of `*rhs.val` is determined by
|
| 417 |
+
the exception safety guarantee of `T`’s constructor. If an exception is
|
| 418 |
thrown during the call to `T`’s assignment, the state of `*val` and
|
| 419 |
`*rhs.val` is determined by the exception safety guarantee of `T`’s
|
| 420 |
assignment.
|
| 421 |
|
| 422 |
``` cpp
|
| 423 |
+
template<class U> constexpr optional<T>& operator=(optional<U>&& rhs);
|
| 424 |
```
|
| 425 |
|
| 426 |
*Constraints:*
|
| 427 |
|
| 428 |
- `is_constructible_v<T, U>` is `true`,
|
| 429 |
- `is_assignable_v<T&, U>` is `true`,
|
| 430 |
+
- *`converts-from-any-cvref`*`<T, optional<U>>` is `false`,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
- `is_assignable_v<T&, optional<U>&>` is `false`,
|
| 432 |
- `is_assignable_v<T&, optional<U>&&>` is `false`,
|
| 433 |
- `is_assignable_v<T&, const optional<U>&>` is `false`, and
|
| 434 |
- `is_assignable_v<T&, const optional<U>&&>` is `false`.
|
| 435 |
|
| 436 |
*Effects:* See [[optional.assign.move.templ]]. The result of the
|
| 437 |
+
expression `rhs.has_value()` remains unchanged.
|
| 438 |
|
| 439 |
**Table: `optional::operator=(optional<U>&&)` effects** <a id="optional.assign.move.templ">[optional.assign.move.templ]</a>
|
| 440 |
|
| 441 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 442 |
+
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 443 |
+
| `rhs` contains a value | assigns `std::move(*rhs)` to the contained value | direct-non-list-initializes the contained value with `std::move(*rhs)` |
|
| 444 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 445 |
|
| 446 |
|
| 447 |
+
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 448 |
|
| 449 |
*Returns:* `*this`.
|
| 450 |
|
| 451 |
*Remarks:* If any exception is thrown, the result of the expression
|
| 452 |
+
`this->has_value()` remains unchanged. If an exception is thrown during
|
| 453 |
+
the call to `T`’s constructor, the state of `*rhs.val` is determined by
|
| 454 |
+
the exception safety guarantee of `T`’s constructor. If an exception is
|
| 455 |
thrown during the call to `T`’s assignment, the state of `*val` and
|
| 456 |
`*rhs.val` is determined by the exception safety guarantee of `T`’s
|
| 457 |
assignment.
|
| 458 |
|
| 459 |
``` cpp
|
| 460 |
+
template<class... Args> constexpr T& emplace(Args&&... args);
|
| 461 |
```
|
| 462 |
|
| 463 |
*Mandates:* `is_constructible_v<T, Args...>` is `true`.
|
| 464 |
|
| 465 |
+
*Effects:* Calls `*this = nullopt`. Then direct-non-list-initializes the
|
| 466 |
+
contained value with `std::forward<Args>(args)...`.
|
|
|
|
| 467 |
|
| 468 |
*Ensures:* `*this` contains a value.
|
| 469 |
|
| 470 |
*Returns:* A reference to the new contained value.
|
| 471 |
|
|
|
|
| 474 |
*Remarks:* If an exception is thrown during the call to `T`’s
|
| 475 |
constructor, `*this` does not contain a value, and the previous `*val`
|
| 476 |
(if any) has been destroyed.
|
| 477 |
|
| 478 |
``` cpp
|
| 479 |
+
template<class U, class... Args> constexpr T& emplace(initializer_list<U> il, Args&&... args);
|
| 480 |
```
|
| 481 |
|
| 482 |
*Constraints:* `is_constructible_v<T, initializer_list<U>&, Args...>` is
|
| 483 |
`true`.
|
| 484 |
|
| 485 |
+
*Effects:* Calls `*this = nullopt`. Then direct-non-list-initializes the
|
| 486 |
+
contained value with `il, std::forward<Args>(args)...`.
|
|
|
|
| 487 |
|
| 488 |
*Ensures:* `*this` contains a value.
|
| 489 |
|
| 490 |
*Returns:* A reference to the new contained value.
|
| 491 |
|
|
|
|
| 496 |
(if any) has been destroyed.
|
| 497 |
|
| 498 |
#### Swap <a id="optional.swap">[[optional.swap]]</a>
|
| 499 |
|
| 500 |
``` cpp
|
| 501 |
+
constexpr void swap(optional& rhs) noexcept(see below);
|
| 502 |
```
|
| 503 |
|
| 504 |
*Mandates:* `is_move_constructible_v<T>` is `true`.
|
| 505 |
|
| 506 |
+
*Preconditions:* `T` meets the *Cpp17Swappable*
|
| 507 |
+
requirements [[swappable.requirements]].
|
| 508 |
|
| 509 |
*Effects:* See [[optional.swap]].
|
| 510 |
|
| 511 |
**Table: `optional::swap(optional&)` effects** <a id="optional.swap">[optional.swap]</a>
|
| 512 |
|
| 513 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 514 |
+
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| 515 |
+
| `rhs` contains a value | calls `swap(*(*this), *rhs)` | direct-non-list-initializes the contained value of `*this` with `std::move(*rhs)`, followed by `rhs.val->T::~T()`; postcondition is that `*this` contains a value and `rhs` does not contain a value |
|
| 516 |
+
| `rhs` does not contain a value | direct-non-list-initializes the contained value of `rhs` with `std::move(*(*this))`, followed by `val->T::~T()`; postcondition is that `*this` does not contain a value and `rhs` contains a value | no effect |
|
| 517 |
|
| 518 |
|
| 519 |
*Throws:* Any exceptions thrown by the operations in the relevant part
|
| 520 |
of [[optional.swap]].
|
| 521 |
|
| 522 |
+
*Remarks:* The exception specification is equivalent to:
|
| 523 |
|
| 524 |
``` cpp
|
| 525 |
is_nothrow_move_constructible_v<T> && is_nothrow_swappable_v<T>
|
| 526 |
```
|
| 527 |
|
| 528 |
+
If any exception is thrown, the results of the expressions
|
| 529 |
+
`this->has_value()` and `rhs.has_value()` remain unchanged. If an
|
| 530 |
+
exception is thrown during the call to function `swap`, the state of
|
| 531 |
+
`*val` and `*rhs.val` is determined by the exception safety guarantee of
|
| 532 |
+
`swap` for lvalues of `T`. If an exception is thrown during the call to
|
| 533 |
+
`T`’s move constructor, the state of `*val` and `*rhs.val` is determined
|
| 534 |
+
by the exception safety guarantee of `T`’s move constructor.
|
| 535 |
|
| 536 |
#### Observers <a id="optional.observe">[[optional.observe]]</a>
|
| 537 |
|
| 538 |
``` cpp
|
| 539 |
+
constexpr const T* operator->() const noexcept;
|
| 540 |
+
constexpr T* operator->() noexcept;
|
| 541 |
```
|
| 542 |
|
| 543 |
*Preconditions:* `*this` contains a value.
|
| 544 |
|
| 545 |
*Returns:* `val`.
|
| 546 |
|
|
|
|
|
|
|
| 547 |
*Remarks:* These functions are constexpr functions.
|
| 548 |
|
| 549 |
``` cpp
|
| 550 |
+
constexpr const T& operator*() const & noexcept;
|
| 551 |
+
constexpr T& operator*() & noexcept;
|
| 552 |
```
|
| 553 |
|
| 554 |
*Preconditions:* `*this` contains a value.
|
| 555 |
|
| 556 |
*Returns:* `*val`.
|
| 557 |
|
|
|
|
|
|
|
| 558 |
*Remarks:* These functions are constexpr functions.
|
| 559 |
|
| 560 |
``` cpp
|
| 561 |
+
constexpr T&& operator*() && noexcept;
|
| 562 |
+
constexpr const T&& operator*() const && noexcept;
|
| 563 |
```
|
| 564 |
|
| 565 |
*Preconditions:* `*this` contains a value.
|
| 566 |
|
| 567 |
*Effects:* Equivalent to: `return std::move(*val);`
|
|
|
|
| 588 |
```
|
| 589 |
|
| 590 |
*Effects:* Equivalent to:
|
| 591 |
|
| 592 |
``` cpp
|
| 593 |
+
return has_value() ? *val : throw bad_optional_access();
|
| 594 |
```
|
| 595 |
|
| 596 |
``` cpp
|
| 597 |
constexpr T&& value() &&;
|
| 598 |
constexpr const T&& value() const &&;
|
| 599 |
```
|
| 600 |
|
| 601 |
*Effects:* Equivalent to:
|
| 602 |
|
| 603 |
``` cpp
|
| 604 |
+
return has_value() ? std::move(*val) : throw bad_optional_access();
|
| 605 |
```
|
| 606 |
|
| 607 |
``` cpp
|
| 608 |
template<class U> constexpr T value_or(U&& v) const &;
|
| 609 |
```
|
|
|
|
| 612 |
`true`.
|
| 613 |
|
| 614 |
*Effects:* Equivalent to:
|
| 615 |
|
| 616 |
``` cpp
|
| 617 |
+
return has_value() ? **this : static_cast<T>(std::forward<U>(v));
|
| 618 |
```
|
| 619 |
|
| 620 |
``` cpp
|
| 621 |
template<class U> constexpr T value_or(U&& v) &&;
|
| 622 |
```
|
|
|
|
| 625 |
`true`.
|
| 626 |
|
| 627 |
*Effects:* Equivalent to:
|
| 628 |
|
| 629 |
``` cpp
|
| 630 |
+
return has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
| 631 |
+
```
|
| 632 |
+
|
| 633 |
+
#### Monadic operations <a id="optional.monadic">[[optional.monadic]]</a>
|
| 634 |
+
|
| 635 |
+
``` cpp
|
| 636 |
+
template<class F> constexpr auto and_then(F&& f) &;
|
| 637 |
+
template<class F> constexpr auto and_then(F&& f) const &;
|
| 638 |
+
```
|
| 639 |
+
|
| 640 |
+
Let `U` be `invoke_result_t<F, decltype(value())>`.
|
| 641 |
+
|
| 642 |
+
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 643 |
+
|
| 644 |
+
*Effects:* Equivalent to:
|
| 645 |
+
|
| 646 |
+
``` cpp
|
| 647 |
+
if (*this) {
|
| 648 |
+
return invoke(std::forward<F>(f), value());
|
| 649 |
+
} else {
|
| 650 |
+
return remove_cvref_t<U>();
|
| 651 |
+
}
|
| 652 |
+
```
|
| 653 |
+
|
| 654 |
+
``` cpp
|
| 655 |
+
template<class F> constexpr auto and_then(F&& f) &&;
|
| 656 |
+
template<class F> constexpr auto and_then(F&& f) const &&;
|
| 657 |
+
```
|
| 658 |
+
|
| 659 |
+
Let `U` be `invoke_result_t<F, decltype(std::move(value()))>`.
|
| 660 |
+
|
| 661 |
+
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 662 |
+
|
| 663 |
+
*Effects:* Equivalent to:
|
| 664 |
+
|
| 665 |
+
``` cpp
|
| 666 |
+
if (*this) {
|
| 667 |
+
return invoke(std::forward<F>(f), std::move(value()));
|
| 668 |
+
} else {
|
| 669 |
+
return remove_cvref_t<U>();
|
| 670 |
+
}
|
| 671 |
+
```
|
| 672 |
+
|
| 673 |
+
``` cpp
|
| 674 |
+
template<class F> constexpr auto transform(F&& f) &;
|
| 675 |
+
template<class F> constexpr auto transform(F&& f) const &;
|
| 676 |
+
```
|
| 677 |
+
|
| 678 |
+
Let `U` be `remove_cv_t<invoke_result_t<F, decltype(value())>>`.
|
| 679 |
+
|
| 680 |
+
*Mandates:* `U` is a non-array object type other than `in_place_t` or
|
| 681 |
+
`nullopt_t`. The declaration
|
| 682 |
+
|
| 683 |
+
``` cpp
|
| 684 |
+
U u(invoke(std::forward<F>(f), value()));
|
| 685 |
+
```
|
| 686 |
+
|
| 687 |
+
is well-formed for some invented variable `u`.
|
| 688 |
+
|
| 689 |
+
[*Note 1*: There is no requirement that `U` is
|
| 690 |
+
movable [[dcl.init.general]]. — *end note*]
|
| 691 |
+
|
| 692 |
+
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 693 |
+
contained value is direct-non-list-initialized with
|
| 694 |
+
`invoke(std::forward<F>(f), value())`; otherwise, `optional<U>()`.
|
| 695 |
+
|
| 696 |
+
``` cpp
|
| 697 |
+
template<class F> constexpr auto transform(F&& f) &&;
|
| 698 |
+
template<class F> constexpr auto transform(F&& f) const &&;
|
| 699 |
+
```
|
| 700 |
+
|
| 701 |
+
Let `U` be
|
| 702 |
+
`remove_cv_t<invoke_result_t<F, decltype(std::move(value()))>>`.
|
| 703 |
+
|
| 704 |
+
*Mandates:* `U` is a non-array object type other than `in_place_t` or
|
| 705 |
+
`nullopt_t`. The declaration
|
| 706 |
+
|
| 707 |
+
``` cpp
|
| 708 |
+
U u(invoke(std::forward<F>(f), std::move(value())));
|
| 709 |
+
```
|
| 710 |
+
|
| 711 |
+
is well-formed for some invented variable `u`.
|
| 712 |
+
|
| 713 |
+
[*Note 2*: There is no requirement that `U` is
|
| 714 |
+
movable [[dcl.init.general]]. — *end note*]
|
| 715 |
+
|
| 716 |
+
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 717 |
+
contained value is direct-non-list-initialized with
|
| 718 |
+
`invoke(std::forward<F>(f), std::move(value()))`; otherwise,
|
| 719 |
+
`optional<U>()`.
|
| 720 |
+
|
| 721 |
+
``` cpp
|
| 722 |
+
template<class F> constexpr optional or_else(F&& f) const &;
|
| 723 |
+
```
|
| 724 |
+
|
| 725 |
+
*Constraints:* `F` models `invocable<>` and `T` models
|
| 726 |
+
`copy_constructible`.
|
| 727 |
+
|
| 728 |
+
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 729 |
+
`true`.
|
| 730 |
+
|
| 731 |
+
*Effects:* Equivalent to:
|
| 732 |
+
|
| 733 |
+
``` cpp
|
| 734 |
+
if (*this) {
|
| 735 |
+
return *this;
|
| 736 |
+
} else {
|
| 737 |
+
return std::forward<F>(f)();
|
| 738 |
+
}
|
| 739 |
+
```
|
| 740 |
+
|
| 741 |
+
``` cpp
|
| 742 |
+
template<class F> constexpr optional or_else(F&& f) &&;
|
| 743 |
+
```
|
| 744 |
+
|
| 745 |
+
*Constraints:* `F` models `invocable<>` and `T` models
|
| 746 |
+
`move_constructible`.
|
| 747 |
+
|
| 748 |
+
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 749 |
+
`true`.
|
| 750 |
+
|
| 751 |
+
*Effects:* Equivalent to:
|
| 752 |
+
|
| 753 |
+
``` cpp
|
| 754 |
+
if (*this) {
|
| 755 |
+
return std::move(*this);
|
| 756 |
+
} else {
|
| 757 |
+
return std::forward<F>(f)();
|
| 758 |
+
}
|
| 759 |
```
|
| 760 |
|
| 761 |
#### Modifiers <a id="optional.mod">[[optional.mod]]</a>
|
| 762 |
|
| 763 |
``` cpp
|
| 764 |
+
constexpr void reset() noexcept;
|
| 765 |
```
|
| 766 |
|
| 767 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 768 |
the contained value; otherwise no effect.
|
| 769 |
|