- tmp/tmpntbzjk4a/{from.md → to.md} +101 -52
tmp/tmpntbzjk4a/{from.md → to.md}
RENAMED
|
@@ -6,21 +6,23 @@
|
|
| 6 |
namespace std {
|
| 7 |
template<class T>
|
| 8 |
class optional {
|
| 9 |
public:
|
| 10 |
using value_type = T;
|
|
|
|
|
|
|
| 11 |
|
| 12 |
// [optional.ctor], constructors
|
| 13 |
constexpr optional() noexcept;
|
| 14 |
constexpr optional(nullopt_t) noexcept;
|
| 15 |
constexpr optional(const optional&);
|
| 16 |
constexpr optional(optional&&) noexcept(see below);
|
| 17 |
template<class... Args>
|
| 18 |
constexpr explicit optional(in_place_t, Args&&...);
|
| 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>&&);
|
|
@@ -30,34 +32,40 @@ namespace std {
|
|
| 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 &;
|
|
@@ -79,25 +87,29 @@ namespace std {
|
|
| 79 |
template<class T>
|
| 80 |
optional(T) -> optional<T>;
|
| 81 |
}
|
| 82 |
```
|
| 83 |
|
| 84 |
-
|
| 85 |
-
or does not contain a value. When an
|
| 86 |
-
a value*, it means that an object of type `T`, referred to as
|
| 87 |
-
optional object’s *contained value*, is
|
| 88 |
-
the optional object.
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
conversion returns `false`.
|
| 93 |
|
| 94 |
-
When an `optional<T>`
|
| 95 |
-
the contained value.
|
| 96 |
|
| 97 |
-
`
|
| 98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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`.
|
|
@@ -142,11 +154,11 @@ 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(
|
| 148 |
|
| 149 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 150 |
|
| 151 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 152 |
|
|
@@ -188,11 +200,11 @@ template<class U, class... Args>
|
|
| 188 |
|
| 189 |
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 190 |
constexpr constructor, this constructor is a constexpr constructor.
|
| 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`,
|
|
@@ -248,11 +260,11 @@ template<class U> constexpr explicit(see below) optional(optional<U>&& rhs);
|
|
| 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(
|
| 254 |
|
| 255 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 256 |
|
| 257 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 258 |
|
|
@@ -332,11 +344,11 @@ constexpr optional& operator=(optional&& rhs) noexcept(see below);
|
|
| 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(
|
| 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 |
|
|
@@ -358,17 +370,19 @@ 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
|
| 364 |
```
|
| 365 |
|
| 366 |
-
*Constraints:*
|
| 367 |
-
|
| 368 |
-
`
|
| 369 |
-
`
|
|
|
|
|
|
|
| 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 |
|
|
@@ -438,11 +452,11 @@ 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(
|
| 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 |
|
|
@@ -531,40 +545,75 @@ 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 |
-
|
| 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 |
-
|
| 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 |
-
|
| 566 |
|
| 567 |
*Effects:* Equivalent to: `return std::move(*val);`
|
| 568 |
|
| 569 |
``` cpp
|
| 570 |
constexpr explicit operator bool() const noexcept;
|
|
@@ -603,11 +652,11 @@ constexpr const T&& value() const &&;
|
|
| 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 |
```
|
| 610 |
|
| 611 |
*Mandates:* `is_copy_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 612 |
`true`.
|
| 613 |
|
|
@@ -616,11 +665,11 @@ template<class U> constexpr T value_or(U&& v) const &;
|
|
| 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 |
```
|
| 623 |
|
| 624 |
*Mandates:* `is_move_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 625 |
`true`.
|
| 626 |
|
|
@@ -635,96 +684,96 @@ return has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
|
| 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(
|
| 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),
|
| 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(
|
| 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(
|
| 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(
|
| 679 |
|
| 680 |
-
*Mandates:* `U` is a
|
| 681 |
-
|
| 682 |
|
| 683 |
``` cpp
|
| 684 |
-
U u(invoke(std::forward<F>(f),
|
| 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),
|
| 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(
|
| 703 |
|
| 704 |
-
*Mandates:* `U` is a
|
| 705 |
-
|
| 706 |
|
| 707 |
``` cpp
|
| 708 |
-
U u(invoke(std::forward<F>(f), std::move(
|
| 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(
|
| 719 |
`optional<U>()`.
|
| 720 |
|
| 721 |
``` cpp
|
| 722 |
template<class F> constexpr optional or_else(F&& f) const &;
|
| 723 |
```
|
| 724 |
|
| 725 |
-
*Constraints:* `F` models `invocable
|
| 726 |
`copy_constructible`.
|
| 727 |
|
| 728 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 729 |
`true`.
|
| 730 |
|
|
@@ -740,11 +789,11 @@ if (*this) {
|
|
| 740 |
|
| 741 |
``` cpp
|
| 742 |
template<class F> constexpr optional or_else(F&& f) &&;
|
| 743 |
```
|
| 744 |
|
| 745 |
-
*Constraints:* `F` models `invocable
|
| 746 |
`move_constructible`.
|
| 747 |
|
| 748 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 749 |
`true`.
|
| 750 |
|
|
|
|
| 6 |
namespace std {
|
| 7 |
template<class T>
|
| 8 |
class optional {
|
| 9 |
public:
|
| 10 |
using value_type = T;
|
| 11 |
+
using iterator = implementation-defined; // see~[optional.iterators]
|
| 12 |
+
using const_iterator = implementation-defined; // see~[optional.iterators]
|
| 13 |
|
| 14 |
// [optional.ctor], constructors
|
| 15 |
constexpr optional() noexcept;
|
| 16 |
constexpr optional(nullopt_t) noexcept;
|
| 17 |
constexpr optional(const optional&);
|
| 18 |
constexpr optional(optional&&) noexcept(see below);
|
| 19 |
template<class... Args>
|
| 20 |
constexpr explicit optional(in_place_t, Args&&...);
|
| 21 |
template<class U, class... Args>
|
| 22 |
constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
|
| 23 |
+
template<class U = remove_cv_t<T>>
|
| 24 |
constexpr explicit(see below) optional(U&&);
|
| 25 |
template<class U>
|
| 26 |
constexpr explicit(see below) optional(const optional<U>&);
|
| 27 |
template<class U>
|
| 28 |
constexpr explicit(see below) optional(optional<U>&&);
|
|
|
|
| 32 |
|
| 33 |
// [optional.assign], assignment
|
| 34 |
constexpr optional& operator=(nullopt_t) noexcept;
|
| 35 |
constexpr optional& operator=(const optional&);
|
| 36 |
constexpr optional& operator=(optional&&) noexcept(see below);
|
| 37 |
+
template<class U = remove_cv_t<T>> constexpr optional& operator=(U&&);
|
| 38 |
template<class U> constexpr optional& operator=(const optional<U>&);
|
| 39 |
template<class U> constexpr optional& operator=(optional<U>&&);
|
| 40 |
template<class... Args> constexpr T& emplace(Args&&...);
|
| 41 |
template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...);
|
| 42 |
|
| 43 |
// [optional.swap], swap
|
| 44 |
constexpr void swap(optional&) noexcept(see below);
|
| 45 |
|
| 46 |
+
// [optional.iterators], iterator support
|
| 47 |
+
constexpr iterator begin() noexcept;
|
| 48 |
+
constexpr const_iterator begin() const noexcept;
|
| 49 |
+
constexpr iterator end() noexcept;
|
| 50 |
+
constexpr const_iterator end() const noexcept;
|
| 51 |
+
|
| 52 |
// [optional.observe], observers
|
| 53 |
constexpr const T* operator->() const noexcept;
|
| 54 |
constexpr T* operator->() noexcept;
|
| 55 |
constexpr const T& operator*() const & noexcept;
|
| 56 |
constexpr T& operator*() & noexcept;
|
| 57 |
constexpr T&& operator*() && noexcept;
|
| 58 |
constexpr const T&& operator*() const && noexcept;
|
| 59 |
constexpr explicit operator bool() const noexcept;
|
| 60 |
constexpr bool has_value() const noexcept;
|
| 61 |
+
constexpr const T& value() const &; // freestanding-deleted
|
| 62 |
+
constexpr T& value() &; // freestanding-deleted
|
| 63 |
+
constexpr T&& value() &&; // freestanding-deleted
|
| 64 |
+
constexpr const T&& value() const &&; // freestanding-deleted
|
| 65 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&&) const &;
|
| 66 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&;
|
| 67 |
|
| 68 |
// [optional.monadic], monadic operations
|
| 69 |
template<class F> constexpr auto and_then(F&& f) &;
|
| 70 |
template<class F> constexpr auto and_then(F&& f) &&;
|
| 71 |
template<class F> constexpr auto and_then(F&& f) const &;
|
|
|
|
| 87 |
template<class T>
|
| 88 |
optional(T) -> optional<T>;
|
| 89 |
}
|
| 90 |
```
|
| 91 |
|
| 92 |
+
An object of type `optional<T>` at any given time either contains a
|
| 93 |
+
value or does not contain a value. When an object of type `optional<T>`
|
| 94 |
+
*contains a value*, it means that an object of type `T`, referred to as
|
| 95 |
+
the optional object’s *contained value*, is nested within
|
| 96 |
+
[[intro.object]] the optional object. When an object of type
|
| 97 |
+
`optional<T>` is contextually converted to `bool`, the conversion
|
| 98 |
+
returns `true` if the object contains a value; otherwise the conversion
|
| 99 |
+
returns `false`.
|
|
|
|
| 100 |
|
| 101 |
+
When an object of type `optional<T>` contains a value, member `val`
|
| 102 |
+
points to the contained value.
|
| 103 |
|
| 104 |
+
A type `X` is a *valid contained type* for `optional` if `X` is an
|
| 105 |
+
lvalue reference type or a complete non-array object type, and
|
| 106 |
+
`remove_cvref_t<X>` is a type other than `in_place_t` or `nullopt_t`. If
|
| 107 |
+
a specialization of `optional` is instantiated with a type `T` that is
|
| 108 |
+
not a valid contained type for `optional`, the program is ill-formed. If
|
| 109 |
+
`T` is an object type, `T` shall meet the *Cpp17Destructible*
|
| 110 |
+
requirements ([[cpp17.destructible]]).
|
| 111 |
|
| 112 |
#### Constructors <a id="optional.ctor">[[optional.ctor]]</a>
|
| 113 |
|
| 114 |
The exposition-only variable template *`converts-from-any-cvref`* is
|
| 115 |
used by some constructors for `optional`.
|
|
|
|
| 154 |
```
|
| 155 |
|
| 156 |
*Constraints:* `is_move_constructible_v<T>` is `true`.
|
| 157 |
|
| 158 |
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 159 |
+
contained value with `*std::move(rhs)`. `rhs.has_value()` is unchanged.
|
| 160 |
|
| 161 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 162 |
|
| 163 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 164 |
|
|
|
|
| 200 |
|
| 201 |
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 202 |
constexpr constructor, this constructor is a constexpr constructor.
|
| 203 |
|
| 204 |
``` cpp
|
| 205 |
+
template<class U = remove_cv_t<T>> constexpr explicit(see below) optional(U&& v);
|
| 206 |
```
|
| 207 |
|
| 208 |
*Constraints:*
|
| 209 |
|
| 210 |
- `is_constructible_v<T, U>` is `true`,
|
|
|
|
| 260 |
- `is_constructible_v<T, U>` is `true`, and
|
| 261 |
- if `T` is not cv `bool`, *`converts-from-any-cvref`*`<T, optional<U>>`
|
| 262 |
is `false`.
|
| 263 |
|
| 264 |
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 265 |
+
contained value with `*std::move(rhs)`. `rhs.has_value()` is unchanged.
|
| 266 |
|
| 267 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 268 |
|
| 269 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 270 |
|
|
|
|
| 344 |
|
| 345 |
**Table: `optional::operator=(optional&&)` effects** <a id="optional.assign.move">[optional.assign.move]</a>
|
| 346 |
|
| 347 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 348 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 349 |
+
| `rhs` contains a value | assigns `*std::move(rhs)` to the contained value | direct-non-list-initializes the contained value with `*std::move(rhs)` |
|
| 350 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 351 |
|
| 352 |
|
| 353 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 354 |
|
|
|
|
| 370 |
`is_trivially_move_constructible_v<T> &&`
|
| 371 |
`is_trivially_move_assignable_v<T> &&` `is_trivially_destructible_v<T>`
|
| 372 |
is `true`, this assignment operator is trivial.
|
| 373 |
|
| 374 |
``` cpp
|
| 375 |
+
template<class U = remove_cv_t<T>> constexpr optional& operator=(U&& v);
|
| 376 |
```
|
| 377 |
|
| 378 |
+
*Constraints:*
|
| 379 |
+
|
| 380 |
+
- `is_same_v<remove_cvref_t<U>, optional>` is `false`,
|
| 381 |
+
- `conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>` is `false`,
|
| 382 |
+
- `is_constructible_v<T, U>` is `true`, and
|
| 383 |
+
- `is_assignable_v<T&, U>` is `true`.
|
| 384 |
|
| 385 |
*Effects:* If `*this` contains a value, assigns `std::forward<U>(v)` to
|
| 386 |
the contained value; otherwise direct-non-list-initializes the contained
|
| 387 |
value with `std::forward<U>(v)`.
|
| 388 |
|
|
|
|
| 452 |
|
| 453 |
**Table: `optional::operator=(optional<U>&&)` effects** <a id="optional.assign.move.templ">[optional.assign.move.templ]</a>
|
| 454 |
|
| 455 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 456 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 457 |
+
| `rhs` contains a value | assigns `*std::move(rhs)` to the contained value | direct-non-list-initializes the contained value with `*std::move(rhs)` |
|
| 458 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 459 |
|
| 460 |
|
| 461 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 462 |
|
|
|
|
| 545 |
`*val` and `*rhs.val` is determined by the exception safety guarantee of
|
| 546 |
`swap` for lvalues of `T`. If an exception is thrown during the call to
|
| 547 |
`T`’s move constructor, the state of `*val` and `*rhs.val` is determined
|
| 548 |
by the exception safety guarantee of `T`’s move constructor.
|
| 549 |
|
| 550 |
+
#### Iterator support <a id="optional.iterators">[[optional.iterators]]</a>
|
| 551 |
+
|
| 552 |
+
``` cpp
|
| 553 |
+
using iterator = implementation-defined;
|
| 554 |
+
using const_iterator = implementation-defined;
|
| 555 |
+
```
|
| 556 |
+
|
| 557 |
+
These types model `contiguous_iterator` [[iterator.concept.contiguous]],
|
| 558 |
+
meet the *Cpp17RandomAccessIterator*
|
| 559 |
+
requirements [[random.access.iterators]], and meet the requirements for
|
| 560 |
+
constexpr iterators [[iterator.requirements.general]], with value type
|
| 561 |
+
`remove_cv_t<T>`. The reference type is `T&` for `iterator` and
|
| 562 |
+
`const T&` for `const_iterator`.
|
| 563 |
+
|
| 564 |
+
All requirements on container iterators [[container.reqmts]] apply to
|
| 565 |
+
`optional::iterator` and `optional::const_iterator` as well.
|
| 566 |
+
|
| 567 |
+
Any operation that initializes or destroys the contained value of an
|
| 568 |
+
optional object invalidates all iterators into that object.
|
| 569 |
+
|
| 570 |
+
``` cpp
|
| 571 |
+
constexpr iterator begin() noexcept;
|
| 572 |
+
constexpr const_iterator begin() const noexcept;
|
| 573 |
+
```
|
| 574 |
+
|
| 575 |
+
*Returns:* If `has_value()` is `true`, an iterator referring to the
|
| 576 |
+
contained value. Otherwise, a past-the-end iterator value.
|
| 577 |
+
|
| 578 |
+
``` cpp
|
| 579 |
+
constexpr iterator end() noexcept;
|
| 580 |
+
constexpr const_iterator end() const noexcept;
|
| 581 |
+
```
|
| 582 |
+
|
| 583 |
+
*Returns:* `begin() + has_value()`.
|
| 584 |
+
|
| 585 |
#### Observers <a id="optional.observe">[[optional.observe]]</a>
|
| 586 |
|
| 587 |
``` cpp
|
| 588 |
constexpr const T* operator->() const noexcept;
|
| 589 |
constexpr T* operator->() noexcept;
|
| 590 |
```
|
| 591 |
|
| 592 |
+
`has_value()` is `true`.
|
| 593 |
|
| 594 |
*Returns:* `val`.
|
| 595 |
|
| 596 |
*Remarks:* These functions are constexpr functions.
|
| 597 |
|
| 598 |
``` cpp
|
| 599 |
constexpr const T& operator*() const & noexcept;
|
| 600 |
constexpr T& operator*() & noexcept;
|
| 601 |
```
|
| 602 |
|
| 603 |
+
`has_value()` is `true`.
|
| 604 |
|
| 605 |
*Returns:* `*val`.
|
| 606 |
|
| 607 |
*Remarks:* These functions are constexpr functions.
|
| 608 |
|
| 609 |
``` cpp
|
| 610 |
constexpr T&& operator*() && noexcept;
|
| 611 |
constexpr const T&& operator*() const && noexcept;
|
| 612 |
```
|
| 613 |
|
| 614 |
+
`has_value()` is `true`.
|
| 615 |
|
| 616 |
*Effects:* Equivalent to: `return std::move(*val);`
|
| 617 |
|
| 618 |
``` cpp
|
| 619 |
constexpr explicit operator bool() const noexcept;
|
|
|
|
| 652 |
``` cpp
|
| 653 |
return has_value() ? std::move(*val) : throw bad_optional_access();
|
| 654 |
```
|
| 655 |
|
| 656 |
``` cpp
|
| 657 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) const &;
|
| 658 |
```
|
| 659 |
|
| 660 |
*Mandates:* `is_copy_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 661 |
`true`.
|
| 662 |
|
|
|
|
| 665 |
``` cpp
|
| 666 |
return has_value() ? **this : static_cast<T>(std::forward<U>(v));
|
| 667 |
```
|
| 668 |
|
| 669 |
``` cpp
|
| 670 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) &&;
|
| 671 |
```
|
| 672 |
|
| 673 |
*Mandates:* `is_move_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 674 |
`true`.
|
| 675 |
|
|
|
|
| 684 |
``` cpp
|
| 685 |
template<class F> constexpr auto and_then(F&& f) &;
|
| 686 |
template<class F> constexpr auto and_then(F&& f) const &;
|
| 687 |
```
|
| 688 |
|
| 689 |
+
Let `U` be `invoke_result_t<F, decltype(*`*`val`*`)>`.
|
| 690 |
|
| 691 |
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 692 |
|
| 693 |
*Effects:* Equivalent to:
|
| 694 |
|
| 695 |
``` cpp
|
| 696 |
if (*this) {
|
| 697 |
+
return invoke(std::forward<F>(f), *val);
|
| 698 |
} else {
|
| 699 |
return remove_cvref_t<U>();
|
| 700 |
}
|
| 701 |
```
|
| 702 |
|
| 703 |
``` cpp
|
| 704 |
template<class F> constexpr auto and_then(F&& f) &&;
|
| 705 |
template<class F> constexpr auto and_then(F&& f) const &&;
|
| 706 |
```
|
| 707 |
|
| 708 |
+
Let `U` be `invoke_result_t<F, decltype(std::move(*`*`val`*`))>`.
|
| 709 |
|
| 710 |
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 711 |
|
| 712 |
*Effects:* Equivalent to:
|
| 713 |
|
| 714 |
``` cpp
|
| 715 |
if (*this) {
|
| 716 |
+
return invoke(std::forward<F>(f), std::move(*val));
|
| 717 |
} else {
|
| 718 |
return remove_cvref_t<U>();
|
| 719 |
}
|
| 720 |
```
|
| 721 |
|
| 722 |
``` cpp
|
| 723 |
template<class F> constexpr auto transform(F&& f) &;
|
| 724 |
template<class F> constexpr auto transform(F&& f) const &;
|
| 725 |
```
|
| 726 |
|
| 727 |
+
Let `U` be `remove_cv_t<invoke_result_t<F, decltype(*`*`val`*`)>>`.
|
| 728 |
|
| 729 |
+
*Mandates:* `U` is a valid contained type for `optional`. The
|
| 730 |
+
declaration
|
| 731 |
|
| 732 |
``` cpp
|
| 733 |
+
U u(invoke(std::forward<F>(f), *val));
|
| 734 |
```
|
| 735 |
|
| 736 |
is well-formed for some invented variable `u`.
|
| 737 |
|
| 738 |
[*Note 1*: There is no requirement that `U` is
|
| 739 |
movable [[dcl.init.general]]. — *end note*]
|
| 740 |
|
| 741 |
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 742 |
contained value is direct-non-list-initialized with
|
| 743 |
+
`invoke(std::forward<F>(f), *`*`val`*`)`; otherwise, `optional<U>()`.
|
| 744 |
|
| 745 |
``` cpp
|
| 746 |
template<class F> constexpr auto transform(F&& f) &&;
|
| 747 |
template<class F> constexpr auto transform(F&& f) const &&;
|
| 748 |
```
|
| 749 |
|
| 750 |
Let `U` be
|
| 751 |
+
`remove_cv_t<invoke_result_t<F, decltype(std::move(*`*`val`*`))>>`.
|
| 752 |
|
| 753 |
+
*Mandates:* `U` is a valid contained type for `optional`. The
|
| 754 |
+
declaration
|
| 755 |
|
| 756 |
``` cpp
|
| 757 |
+
U u(invoke(std::forward<F>(f), std::move(*val)));
|
| 758 |
```
|
| 759 |
|
| 760 |
is well-formed for some invented variable `u`.
|
| 761 |
|
| 762 |
[*Note 2*: There is no requirement that `U` is
|
| 763 |
movable [[dcl.init.general]]. — *end note*]
|
| 764 |
|
| 765 |
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 766 |
contained value is direct-non-list-initialized with
|
| 767 |
+
`invoke(std::forward<F>(f), std::move(*`*`val`*`))`; otherwise,
|
| 768 |
`optional<U>()`.
|
| 769 |
|
| 770 |
``` cpp
|
| 771 |
template<class F> constexpr optional or_else(F&& f) const &;
|
| 772 |
```
|
| 773 |
|
| 774 |
+
*Constraints:* `F` models `invocable` and `T` models
|
| 775 |
`copy_constructible`.
|
| 776 |
|
| 777 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 778 |
`true`.
|
| 779 |
|
|
|
|
| 789 |
|
| 790 |
``` cpp
|
| 791 |
template<class F> constexpr optional or_else(F&& f) &&;
|
| 792 |
```
|
| 793 |
|
| 794 |
+
*Constraints:* `F` models `invocable` and `T` models
|
| 795 |
`move_constructible`.
|
| 796 |
|
| 797 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 798 |
`true`.
|
| 799 |
|