- tmp/tmpv8q148a5/{from.md → to.md} +586 -94
tmp/tmpv8q148a5/{from.md → to.md}
RENAMED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
## Optional objects <a id="optional">[[optional]]</a>
|
| 2 |
|
| 3 |
-
###
|
| 4 |
|
| 5 |
Subclause [[optional]] describes class template `optional` that
|
| 6 |
represents optional objects. An *optional object* is an object that
|
| 7 |
contains the storage for another object and manages the lifetime of this
|
| 8 |
contained object, if any. The contained object may be initialized after
|
|
@@ -11,16 +11,28 @@ the optional object has been destroyed. The initialization state of the
|
|
| 11 |
contained object is tracked by the optional object.
|
| 12 |
|
| 13 |
### Header `<optional>` synopsis <a id="optional.syn">[[optional.syn]]</a>
|
| 14 |
|
| 15 |
``` cpp
|
|
|
|
| 16 |
#include <compare> // see [compare.syn]
|
| 17 |
|
| 18 |
namespace std {
|
| 19 |
// [optional.optional], class template optional
|
| 20 |
template<class T>
|
| 21 |
-
class optional;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
template<class T>
|
| 24 |
concept is-derived-from-optional = requires(const T& t) { // exposition only
|
| 25 |
[]<class U>(const optional<U>&){ }(t);
|
| 26 |
};
|
|
@@ -75,11 +87,11 @@ namespace std {
|
|
| 75 |
// [optional.specalg], specialized algorithms
|
| 76 |
template<class T>
|
| 77 |
constexpr void swap(optional<T>&, optional<T>&) noexcept(see below);
|
| 78 |
|
| 79 |
template<class T>
|
| 80 |
-
constexpr optional<
|
| 81 |
template<class T, class... Args>
|
| 82 |
constexpr optional<T> make_optional(Args&&... args);
|
| 83 |
template<class T, class U, class... Args>
|
| 84 |
constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
|
| 85 |
|
|
@@ -97,21 +109,23 @@ namespace std {
|
|
| 97 |
namespace std {
|
| 98 |
template<class T>
|
| 99 |
class optional {
|
| 100 |
public:
|
| 101 |
using value_type = T;
|
|
|
|
|
|
|
| 102 |
|
| 103 |
// [optional.ctor], constructors
|
| 104 |
constexpr optional() noexcept;
|
| 105 |
constexpr optional(nullopt_t) noexcept;
|
| 106 |
constexpr optional(const optional&);
|
| 107 |
constexpr optional(optional&&) noexcept(see below);
|
| 108 |
template<class... Args>
|
| 109 |
constexpr explicit optional(in_place_t, Args&&...);
|
| 110 |
template<class U, class... Args>
|
| 111 |
constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
|
| 112 |
-
template<class U = T>
|
| 113 |
constexpr explicit(see below) optional(U&&);
|
| 114 |
template<class U>
|
| 115 |
constexpr explicit(see below) optional(const optional<U>&);
|
| 116 |
template<class U>
|
| 117 |
constexpr explicit(see below) optional(optional<U>&&);
|
|
@@ -121,34 +135,40 @@ namespace std {
|
|
| 121 |
|
| 122 |
// [optional.assign], assignment
|
| 123 |
constexpr optional& operator=(nullopt_t) noexcept;
|
| 124 |
constexpr optional& operator=(const optional&);
|
| 125 |
constexpr optional& operator=(optional&&) noexcept(see below);
|
| 126 |
-
template<class U = T> constexpr optional& operator=(U&&);
|
| 127 |
template<class U> constexpr optional& operator=(const optional<U>&);
|
| 128 |
template<class U> constexpr optional& operator=(optional<U>&&);
|
| 129 |
template<class... Args> constexpr T& emplace(Args&&...);
|
| 130 |
template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...);
|
| 131 |
|
| 132 |
// [optional.swap], swap
|
| 133 |
constexpr void swap(optional&) noexcept(see below);
|
| 134 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
// [optional.observe], observers
|
| 136 |
constexpr const T* operator->() const noexcept;
|
| 137 |
constexpr T* operator->() noexcept;
|
| 138 |
constexpr const T& operator*() const & noexcept;
|
| 139 |
constexpr T& operator*() & noexcept;
|
| 140 |
constexpr T&& operator*() && noexcept;
|
| 141 |
constexpr const T&& operator*() const && noexcept;
|
| 142 |
constexpr explicit operator bool() const noexcept;
|
| 143 |
constexpr bool has_value() const noexcept;
|
| 144 |
-
constexpr const T& value() const &;
|
| 145 |
-
constexpr T& value() &;
|
| 146 |
-
constexpr T&& value() &&;
|
| 147 |
-
constexpr const T&& value() const &&;
|
| 148 |
-
template<class U> constexpr T value_or(U&&) const &;
|
| 149 |
-
template<class U> constexpr T value_or(U&&) &&;
|
| 150 |
|
| 151 |
// [optional.monadic], monadic operations
|
| 152 |
template<class F> constexpr auto and_then(F&& f) &;
|
| 153 |
template<class F> constexpr auto and_then(F&& f) &&;
|
| 154 |
template<class F> constexpr auto and_then(F&& f) const &;
|
|
@@ -170,25 +190,29 @@ namespace std {
|
|
| 170 |
template<class T>
|
| 171 |
optional(T) -> optional<T>;
|
| 172 |
}
|
| 173 |
```
|
| 174 |
|
| 175 |
-
|
| 176 |
-
or does not contain a value. When an
|
| 177 |
-
a value*, it means that an object of type `T`, referred to as
|
| 178 |
-
optional object’s *contained value*, is
|
| 179 |
-
the optional object.
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
conversion returns `false`.
|
| 184 |
|
| 185 |
-
When an `optional<T>`
|
| 186 |
-
the contained value.
|
| 187 |
|
| 188 |
-
`
|
| 189 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
|
| 191 |
#### Constructors <a id="optional.ctor">[[optional.ctor]]</a>
|
| 192 |
|
| 193 |
The exposition-only variable template *`converts-from-any-cvref`* is
|
| 194 |
used by some constructors for `optional`.
|
|
@@ -233,11 +257,11 @@ constexpr optional(optional&& rhs) noexcept(see below);
|
|
| 233 |
```
|
| 234 |
|
| 235 |
*Constraints:* `is_move_constructible_v<T>` is `true`.
|
| 236 |
|
| 237 |
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 238 |
-
contained value with `std::move(
|
| 239 |
|
| 240 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 241 |
|
| 242 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 243 |
|
|
@@ -279,11 +303,11 @@ template<class U, class... Args>
|
|
| 279 |
|
| 280 |
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 281 |
constexpr constructor, this constructor is a constexpr constructor.
|
| 282 |
|
| 283 |
``` cpp
|
| 284 |
-
template<class U = T> constexpr explicit(see below) optional(U&& v);
|
| 285 |
```
|
| 286 |
|
| 287 |
*Constraints:*
|
| 288 |
|
| 289 |
- `is_constructible_v<T, U>` is `true`,
|
|
@@ -339,11 +363,11 @@ template<class U> constexpr explicit(see below) optional(optional<U>&& rhs);
|
|
| 339 |
- `is_constructible_v<T, U>` is `true`, and
|
| 340 |
- if `T` is not cv `bool`, *`converts-from-any-cvref`*`<T, optional<U>>`
|
| 341 |
is `false`.
|
| 342 |
|
| 343 |
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 344 |
-
contained value with `std::move(
|
| 345 |
|
| 346 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 347 |
|
| 348 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 349 |
|
|
@@ -423,11 +447,11 @@ constexpr optional& operator=(optional&& rhs) noexcept(see below);
|
|
| 423 |
|
| 424 |
**Table: `optional::operator=(optional&&)` effects** <a id="optional.assign.move">[optional.assign.move]</a>
|
| 425 |
|
| 426 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 427 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 428 |
-
| `rhs` contains a value | assigns `std::move(
|
| 429 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 430 |
|
| 431 |
|
| 432 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 433 |
|
|
@@ -449,17 +473,19 @@ guarantee of `T`’s move assignment. If
|
|
| 449 |
`is_trivially_move_constructible_v<T> &&`
|
| 450 |
`is_trivially_move_assignable_v<T> &&` `is_trivially_destructible_v<T>`
|
| 451 |
is `true`, this assignment operator is trivial.
|
| 452 |
|
| 453 |
``` cpp
|
| 454 |
-
template<class U = T> constexpr optional
|
| 455 |
```
|
| 456 |
|
| 457 |
-
*Constraints:*
|
| 458 |
-
|
| 459 |
-
`
|
| 460 |
-
`
|
|
|
|
|
|
|
| 461 |
|
| 462 |
*Effects:* If `*this` contains a value, assigns `std::forward<U>(v)` to
|
| 463 |
the contained value; otherwise direct-non-list-initializes the contained
|
| 464 |
value with `std::forward<U>(v)`.
|
| 465 |
|
|
@@ -529,11 +555,11 @@ expression `rhs.has_value()` remains unchanged.
|
|
| 529 |
|
| 530 |
**Table: `optional::operator=(optional<U>&&)` effects** <a id="optional.assign.move.templ">[optional.assign.move.templ]</a>
|
| 531 |
|
| 532 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 533 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 534 |
-
| `rhs` contains a value | assigns `std::move(
|
| 535 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 536 |
|
| 537 |
|
| 538 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 539 |
|
|
@@ -622,40 +648,75 @@ exception is thrown during the call to function `swap`, the state of
|
|
| 622 |
`*val` and `*rhs.val` is determined by the exception safety guarantee of
|
| 623 |
`swap` for lvalues of `T`. If an exception is thrown during the call to
|
| 624 |
`T`’s move constructor, the state of `*val` and `*rhs.val` is determined
|
| 625 |
by the exception safety guarantee of `T`’s move constructor.
|
| 626 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 627 |
#### Observers <a id="optional.observe">[[optional.observe]]</a>
|
| 628 |
|
| 629 |
``` cpp
|
| 630 |
constexpr const T* operator->() const noexcept;
|
| 631 |
constexpr T* operator->() noexcept;
|
| 632 |
```
|
| 633 |
|
| 634 |
-
|
| 635 |
|
| 636 |
*Returns:* `val`.
|
| 637 |
|
| 638 |
*Remarks:* These functions are constexpr functions.
|
| 639 |
|
| 640 |
``` cpp
|
| 641 |
constexpr const T& operator*() const & noexcept;
|
| 642 |
constexpr T& operator*() & noexcept;
|
| 643 |
```
|
| 644 |
|
| 645 |
-
|
| 646 |
|
| 647 |
*Returns:* `*val`.
|
| 648 |
|
| 649 |
*Remarks:* These functions are constexpr functions.
|
| 650 |
|
| 651 |
``` cpp
|
| 652 |
constexpr T&& operator*() && noexcept;
|
| 653 |
constexpr const T&& operator*() const && noexcept;
|
| 654 |
```
|
| 655 |
|
| 656 |
-
|
| 657 |
|
| 658 |
*Effects:* Equivalent to: `return std::move(*val);`
|
| 659 |
|
| 660 |
``` cpp
|
| 661 |
constexpr explicit operator bool() const noexcept;
|
|
@@ -694,11 +755,11 @@ constexpr const T&& value() const &&;
|
|
| 694 |
``` cpp
|
| 695 |
return has_value() ? std::move(*val) : throw bad_optional_access();
|
| 696 |
```
|
| 697 |
|
| 698 |
``` cpp
|
| 699 |
-
template<class U> constexpr T value_or(U&& v) const &;
|
| 700 |
```
|
| 701 |
|
| 702 |
*Mandates:* `is_copy_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 703 |
`true`.
|
| 704 |
|
|
@@ -707,11 +768,11 @@ template<class U> constexpr T value_or(U&& v) const &;
|
|
| 707 |
``` cpp
|
| 708 |
return has_value() ? **this : static_cast<T>(std::forward<U>(v));
|
| 709 |
```
|
| 710 |
|
| 711 |
``` cpp
|
| 712 |
-
template<class U> constexpr T value_or(U&& v) &&;
|
| 713 |
```
|
| 714 |
|
| 715 |
*Mandates:* `is_move_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 716 |
`true`.
|
| 717 |
|
|
@@ -726,96 +787,96 @@ return has_value() ? std::move(**this) : static_cast<T>(std::forward<U>(v));
|
|
| 726 |
``` cpp
|
| 727 |
template<class F> constexpr auto and_then(F&& f) &;
|
| 728 |
template<class F> constexpr auto and_then(F&& f) const &;
|
| 729 |
```
|
| 730 |
|
| 731 |
-
Let `U` be `invoke_result_t<F, decltype(
|
| 732 |
|
| 733 |
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 734 |
|
| 735 |
*Effects:* Equivalent to:
|
| 736 |
|
| 737 |
``` cpp
|
| 738 |
if (*this) {
|
| 739 |
-
return invoke(std::forward<F>(f),
|
| 740 |
} else {
|
| 741 |
return remove_cvref_t<U>();
|
| 742 |
}
|
| 743 |
```
|
| 744 |
|
| 745 |
``` cpp
|
| 746 |
template<class F> constexpr auto and_then(F&& f) &&;
|
| 747 |
template<class F> constexpr auto and_then(F&& f) const &&;
|
| 748 |
```
|
| 749 |
|
| 750 |
-
Let `U` be `invoke_result_t<F, decltype(std::move(
|
| 751 |
|
| 752 |
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 753 |
|
| 754 |
*Effects:* Equivalent to:
|
| 755 |
|
| 756 |
``` cpp
|
| 757 |
if (*this) {
|
| 758 |
-
return invoke(std::forward<F>(f), std::move(
|
| 759 |
} else {
|
| 760 |
return remove_cvref_t<U>();
|
| 761 |
}
|
| 762 |
```
|
| 763 |
|
| 764 |
``` cpp
|
| 765 |
template<class F> constexpr auto transform(F&& f) &;
|
| 766 |
template<class F> constexpr auto transform(F&& f) const &;
|
| 767 |
```
|
| 768 |
|
| 769 |
-
Let `U` be `remove_cv_t<invoke_result_t<F, decltype(
|
| 770 |
|
| 771 |
-
*Mandates:* `U` is a
|
| 772 |
-
|
| 773 |
|
| 774 |
``` cpp
|
| 775 |
-
U u(invoke(std::forward<F>(f),
|
| 776 |
```
|
| 777 |
|
| 778 |
is well-formed for some invented variable `u`.
|
| 779 |
|
| 780 |
[*Note 1*: There is no requirement that `U` is
|
| 781 |
movable [[dcl.init.general]]. — *end note*]
|
| 782 |
|
| 783 |
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 784 |
contained value is direct-non-list-initialized with
|
| 785 |
-
`invoke(std::forward<F>(f),
|
| 786 |
|
| 787 |
``` cpp
|
| 788 |
template<class F> constexpr auto transform(F&& f) &&;
|
| 789 |
template<class F> constexpr auto transform(F&& f) const &&;
|
| 790 |
```
|
| 791 |
|
| 792 |
Let `U` be
|
| 793 |
-
`remove_cv_t<invoke_result_t<F, decltype(std::move(
|
| 794 |
|
| 795 |
-
*Mandates:* `U` is a
|
| 796 |
-
|
| 797 |
|
| 798 |
``` cpp
|
| 799 |
-
U u(invoke(std::forward<F>(f), std::move(
|
| 800 |
```
|
| 801 |
|
| 802 |
is well-formed for some invented variable `u`.
|
| 803 |
|
| 804 |
[*Note 2*: There is no requirement that `U` is
|
| 805 |
movable [[dcl.init.general]]. — *end note*]
|
| 806 |
|
| 807 |
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 808 |
contained value is direct-non-list-initialized with
|
| 809 |
-
`invoke(std::forward<F>(f), std::move(
|
| 810 |
`optional<U>()`.
|
| 811 |
|
| 812 |
``` cpp
|
| 813 |
template<class F> constexpr optional or_else(F&& f) const &;
|
| 814 |
```
|
| 815 |
|
| 816 |
-
*Constraints:* `F` models `invocable
|
| 817 |
`copy_constructible`.
|
| 818 |
|
| 819 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 820 |
`true`.
|
| 821 |
|
|
@@ -831,11 +892,11 @@ if (*this) {
|
|
| 831 |
|
| 832 |
``` cpp
|
| 833 |
template<class F> constexpr optional or_else(F&& f) &&;
|
| 834 |
```
|
| 835 |
|
| 836 |
-
*Constraints:* `F` models `invocable
|
| 837 |
`move_constructible`.
|
| 838 |
|
| 839 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 840 |
`true`.
|
| 841 |
|
|
@@ -858,10 +919,432 @@ constexpr void reset() noexcept;
|
|
| 858 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 859 |
the contained value; otherwise no effect.
|
| 860 |
|
| 861 |
*Ensures:* `*this` does not contain a value.
|
| 862 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 863 |
### No-value state indicator <a id="optional.nullopt">[[optional.nullopt]]</a>
|
| 864 |
|
| 865 |
``` cpp
|
| 866 |
struct nullopt_t{see below};
|
| 867 |
inline constexpr nullopt_t nullopt(unspecified);
|
|
@@ -881,33 +1364,34 @@ initializer-list constructor, and shall not be an aggregate.
|
|
| 881 |
``` cpp
|
| 882 |
namespace std {
|
| 883 |
class bad_optional_access : public exception {
|
| 884 |
public:
|
| 885 |
// see [exception] for the specification of the special member functions
|
| 886 |
-
const char* what() const noexcept override;
|
| 887 |
};
|
| 888 |
}
|
| 889 |
```
|
| 890 |
|
| 891 |
The class `bad_optional_access` defines the type of objects thrown as
|
| 892 |
exceptions to report the situation where an attempt is made to access
|
| 893 |
the value of an optional object that does not contain a value.
|
| 894 |
|
| 895 |
``` cpp
|
| 896 |
-
const char* what() const noexcept override;
|
| 897 |
```
|
| 898 |
|
| 899 |
-
*Returns:* An *implementation-defined* NTBS
|
|
|
|
| 900 |
|
| 901 |
### Relational operators <a id="optional.relops">[[optional.relops]]</a>
|
| 902 |
|
| 903 |
``` cpp
|
| 904 |
template<class T, class U> constexpr bool operator==(const optional<T>& x, const optional<U>& y);
|
| 905 |
```
|
| 906 |
|
| 907 |
-
*
|
| 908 |
-
convertible to `bool`.
|
| 909 |
|
| 910 |
[*Note 1*: `T` need not be *Cpp17EqualityComparable*. — *end note*]
|
| 911 |
|
| 912 |
*Returns:* If `x.has_value() != y.has_value()`, `false`; otherwise if
|
| 913 |
`x.has_value() == false`, `true`; otherwise `*x == *y`.
|
|
@@ -917,12 +1401,12 @@ convertible to `bool`.
|
|
| 917 |
|
| 918 |
``` cpp
|
| 919 |
template<class T, class U> constexpr bool operator!=(const optional<T>& x, const optional<U>& y);
|
| 920 |
```
|
| 921 |
|
| 922 |
-
*
|
| 923 |
-
convertible to `bool`.
|
| 924 |
|
| 925 |
*Returns:* If `x.has_value() != y.has_value()`, `true`; otherwise, if
|
| 926 |
`x.has_value() == false`, `false`; otherwise `*x != *y`.
|
| 927 |
|
| 928 |
*Remarks:* Specializations of this function template for which
|
|
@@ -930,11 +1414,11 @@ convertible to `bool`.
|
|
| 930 |
|
| 931 |
``` cpp
|
| 932 |
template<class T, class U> constexpr bool operator<(const optional<T>& x, const optional<U>& y);
|
| 933 |
```
|
| 934 |
|
| 935 |
-
*
|
| 936 |
`bool`.
|
| 937 |
|
| 938 |
*Returns:* If `!y`, `false`; otherwise, if `!x`, `true`; otherwise
|
| 939 |
`*x < *y`.
|
| 940 |
|
|
@@ -943,11 +1427,11 @@ is a core constant expression are constexpr functions.
|
|
| 943 |
|
| 944 |
``` cpp
|
| 945 |
template<class T, class U> constexpr bool operator>(const optional<T>& x, const optional<U>& y);
|
| 946 |
```
|
| 947 |
|
| 948 |
-
*
|
| 949 |
convertible to `bool`.
|
| 950 |
|
| 951 |
*Returns:* If `!x`, `false`; otherwise, if `!y`, `true`; otherwise
|
| 952 |
`*x > *y`.
|
| 953 |
|
|
@@ -956,12 +1440,12 @@ is a core constant expression are constexpr functions.
|
|
| 956 |
|
| 957 |
``` cpp
|
| 958 |
template<class T, class U> constexpr bool operator<=(const optional<T>& x, const optional<U>& y);
|
| 959 |
```
|
| 960 |
|
| 961 |
-
*
|
| 962 |
-
convertible to `bool`.
|
| 963 |
|
| 964 |
*Returns:* If `!x`, `true`; otherwise, if `!y`, `false`; otherwise
|
| 965 |
`*x <= *y`.
|
| 966 |
|
| 967 |
*Remarks:* Specializations of this function template for which
|
|
@@ -969,12 +1453,12 @@ convertible to `bool`.
|
|
| 969 |
|
| 970 |
``` cpp
|
| 971 |
template<class T, class U> constexpr bool operator>=(const optional<T>& x, const optional<U>& y);
|
| 972 |
```
|
| 973 |
|
| 974 |
-
*
|
| 975 |
-
convertible to `bool`.
|
| 976 |
|
| 977 |
*Returns:* If `!y`, `true`; otherwise, if `!x`, `false`; otherwise
|
| 978 |
`*x >= *y`.
|
| 979 |
|
| 980 |
*Remarks:* Specializations of this function template for which
|
|
@@ -1010,113 +1494,113 @@ template<class T> constexpr strong_ordering operator<=>(const optional<T>& x, nu
|
|
| 1010 |
|
| 1011 |
``` cpp
|
| 1012 |
template<class T, class U> constexpr bool operator==(const optional<T>& x, const U& v);
|
| 1013 |
```
|
| 1014 |
|
| 1015 |
-
*
|
| 1016 |
-
convertible to `bool`.
|
| 1017 |
|
| 1018 |
[*Note 1*: `T` need not be *Cpp17EqualityComparable*. — *end note*]
|
| 1019 |
|
| 1020 |
*Effects:* Equivalent to: `return x.has_value() ? *x == v : false;`
|
| 1021 |
|
| 1022 |
``` cpp
|
| 1023 |
template<class T, class U> constexpr bool operator==(const T& v, const optional<U>& x);
|
| 1024 |
```
|
| 1025 |
|
| 1026 |
-
*
|
| 1027 |
-
convertible to `bool`.
|
| 1028 |
|
| 1029 |
*Effects:* Equivalent to: `return x.has_value() ? v == *x : false;`
|
| 1030 |
|
| 1031 |
``` cpp
|
| 1032 |
template<class T, class U> constexpr bool operator!=(const optional<T>& x, const U& v);
|
| 1033 |
```
|
| 1034 |
|
| 1035 |
-
*
|
| 1036 |
-
convertible to `bool`.
|
| 1037 |
|
| 1038 |
*Effects:* Equivalent to: `return x.has_value() ? *x != v : true;`
|
| 1039 |
|
| 1040 |
``` cpp
|
| 1041 |
template<class T, class U> constexpr bool operator!=(const T& v, const optional<U>& x);
|
| 1042 |
```
|
| 1043 |
|
| 1044 |
-
*
|
| 1045 |
-
convertible to `bool`.
|
| 1046 |
|
| 1047 |
*Effects:* Equivalent to: `return x.has_value() ? v != *x : true;`
|
| 1048 |
|
| 1049 |
``` cpp
|
| 1050 |
template<class T, class U> constexpr bool operator<(const optional<T>& x, const U& v);
|
| 1051 |
```
|
| 1052 |
|
| 1053 |
-
*
|
| 1054 |
-
convertible to `bool`.
|
| 1055 |
|
| 1056 |
*Effects:* Equivalent to: `return x.has_value() ? *x < v : true;`
|
| 1057 |
|
| 1058 |
``` cpp
|
| 1059 |
template<class T, class U> constexpr bool operator<(const T& v, const optional<U>& x);
|
| 1060 |
```
|
| 1061 |
|
| 1062 |
-
*
|
| 1063 |
-
convertible to `bool`.
|
| 1064 |
|
| 1065 |
*Effects:* Equivalent to: `return x.has_value() ? v < *x : false;`
|
| 1066 |
|
| 1067 |
``` cpp
|
| 1068 |
template<class T, class U> constexpr bool operator>(const optional<T>& x, const U& v);
|
| 1069 |
```
|
| 1070 |
|
| 1071 |
-
*
|
| 1072 |
-
convertible to `bool`.
|
| 1073 |
|
| 1074 |
*Effects:* Equivalent to: `return x.has_value() ? *x > v : false;`
|
| 1075 |
|
| 1076 |
``` cpp
|
| 1077 |
template<class T, class U> constexpr bool operator>(const T& v, const optional<U>& x);
|
| 1078 |
```
|
| 1079 |
|
| 1080 |
-
*
|
| 1081 |
-
convertible to `bool`.
|
| 1082 |
|
| 1083 |
*Effects:* Equivalent to: `return x.has_value() ? v > *x : true;`
|
| 1084 |
|
| 1085 |
``` cpp
|
| 1086 |
template<class T, class U> constexpr bool operator<=(const optional<T>& x, const U& v);
|
| 1087 |
```
|
| 1088 |
|
| 1089 |
-
*
|
| 1090 |
-
convertible to `bool`.
|
| 1091 |
|
| 1092 |
*Effects:* Equivalent to: `return x.has_value() ? *x <= v : true;`
|
| 1093 |
|
| 1094 |
``` cpp
|
| 1095 |
template<class T, class U> constexpr bool operator<=(const T& v, const optional<U>& x);
|
| 1096 |
```
|
| 1097 |
|
| 1098 |
-
*
|
| 1099 |
-
convertible to `bool`.
|
| 1100 |
|
| 1101 |
*Effects:* Equivalent to: `return x.has_value() ? v <= *x : false;`
|
| 1102 |
|
| 1103 |
``` cpp
|
| 1104 |
template<class T, class U> constexpr bool operator>=(const optional<T>& x, const U& v);
|
| 1105 |
```
|
| 1106 |
|
| 1107 |
-
*
|
| 1108 |
-
convertible to `bool`.
|
| 1109 |
|
| 1110 |
*Effects:* Equivalent to: `return x.has_value() ? *x >= v : false;`
|
| 1111 |
|
| 1112 |
``` cpp
|
| 1113 |
template<class T, class U> constexpr bool operator>=(const T& v, const optional<U>& x);
|
| 1114 |
```
|
| 1115 |
|
| 1116 |
-
*
|
| 1117 |
-
convertible to `bool`.
|
| 1118 |
|
| 1119 |
*Effects:* Equivalent to: `return x.has_value() ? v >= *x : true;`
|
| 1120 |
|
| 1121 |
``` cpp
|
| 1122 |
template<class T, class U>
|
|
@@ -1133,19 +1617,27 @@ template<class T, class U>
|
|
| 1133 |
``` cpp
|
| 1134 |
template<class T>
|
| 1135 |
constexpr void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y)));
|
| 1136 |
```
|
| 1137 |
|
| 1138 |
-
*Constraints:*
|
| 1139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1140 |
|
| 1141 |
*Effects:* Calls `x.swap(y)`.
|
| 1142 |
|
| 1143 |
``` cpp
|
| 1144 |
template<class T> constexpr optional<decay_t<T>> make_optional(T&& v);
|
| 1145 |
```
|
| 1146 |
|
|
|
|
|
|
|
|
|
|
| 1147 |
*Returns:* `optional<decay_t<T>>(std::forward<T>(v))`.
|
| 1148 |
|
| 1149 |
``` cpp
|
| 1150 |
template<class T, class...Args>
|
| 1151 |
constexpr optional<T> make_optional(Args&&... args);
|
|
|
|
| 1 |
## Optional objects <a id="optional">[[optional]]</a>
|
| 2 |
|
| 3 |
+
### General <a id="optional.general">[[optional.general]]</a>
|
| 4 |
|
| 5 |
Subclause [[optional]] describes class template `optional` that
|
| 6 |
represents optional objects. An *optional object* is an object that
|
| 7 |
contains the storage for another object and manages the lifetime of this
|
| 8 |
contained object, if any. The contained object may be initialized after
|
|
|
|
| 11 |
contained object is tracked by the optional object.
|
| 12 |
|
| 13 |
### Header `<optional>` synopsis <a id="optional.syn">[[optional.syn]]</a>
|
| 14 |
|
| 15 |
``` cpp
|
| 16 |
+
// mostly freestanding
|
| 17 |
#include <compare> // see [compare.syn]
|
| 18 |
|
| 19 |
namespace std {
|
| 20 |
// [optional.optional], class template optional
|
| 21 |
template<class T>
|
| 22 |
+
class optional; // partially freestanding
|
| 23 |
+
|
| 24 |
+
// [optional.optional.ref], partial specialization of optional for lvalue reference types
|
| 25 |
+
template<class T>
|
| 26 |
+
class optional<T&>; // partially freestanding
|
| 27 |
+
|
| 28 |
+
template<class T>
|
| 29 |
+
constexpr bool ranges::enable_view<optional<T>> = true;
|
| 30 |
+
template<class T>
|
| 31 |
+
constexpr auto format_kind<optional<T>> = range_format::disabled;
|
| 32 |
+
template<class T>
|
| 33 |
+
constexpr bool ranges::enable_borrowed_range<optional<T&>> = true;
|
| 34 |
|
| 35 |
template<class T>
|
| 36 |
concept is-derived-from-optional = requires(const T& t) { // exposition only
|
| 37 |
[]<class U>(const optional<U>&){ }(t);
|
| 38 |
};
|
|
|
|
| 87 |
// [optional.specalg], specialized algorithms
|
| 88 |
template<class T>
|
| 89 |
constexpr void swap(optional<T>&, optional<T>&) noexcept(see below);
|
| 90 |
|
| 91 |
template<class T>
|
| 92 |
+
constexpr optional<decay_t<T>> make_optional(T&&);
|
| 93 |
template<class T, class... Args>
|
| 94 |
constexpr optional<T> make_optional(Args&&... args);
|
| 95 |
template<class T, class U, class... Args>
|
| 96 |
constexpr optional<T> make_optional(initializer_list<U> il, Args&&... args);
|
| 97 |
|
|
|
|
| 109 |
namespace std {
|
| 110 |
template<class T>
|
| 111 |
class optional {
|
| 112 |
public:
|
| 113 |
using value_type = T;
|
| 114 |
+
using iterator = implementation-defined; // see~[optional.iterators]
|
| 115 |
+
using const_iterator = implementation-defined; // see~[optional.iterators]
|
| 116 |
|
| 117 |
// [optional.ctor], constructors
|
| 118 |
constexpr optional() noexcept;
|
| 119 |
constexpr optional(nullopt_t) noexcept;
|
| 120 |
constexpr optional(const optional&);
|
| 121 |
constexpr optional(optional&&) noexcept(see below);
|
| 122 |
template<class... Args>
|
| 123 |
constexpr explicit optional(in_place_t, Args&&...);
|
| 124 |
template<class U, class... Args>
|
| 125 |
constexpr explicit optional(in_place_t, initializer_list<U>, Args&&...);
|
| 126 |
+
template<class U = remove_cv_t<T>>
|
| 127 |
constexpr explicit(see below) optional(U&&);
|
| 128 |
template<class U>
|
| 129 |
constexpr explicit(see below) optional(const optional<U>&);
|
| 130 |
template<class U>
|
| 131 |
constexpr explicit(see below) optional(optional<U>&&);
|
|
|
|
| 135 |
|
| 136 |
// [optional.assign], assignment
|
| 137 |
constexpr optional& operator=(nullopt_t) noexcept;
|
| 138 |
constexpr optional& operator=(const optional&);
|
| 139 |
constexpr optional& operator=(optional&&) noexcept(see below);
|
| 140 |
+
template<class U = remove_cv_t<T>> constexpr optional& operator=(U&&);
|
| 141 |
template<class U> constexpr optional& operator=(const optional<U>&);
|
| 142 |
template<class U> constexpr optional& operator=(optional<U>&&);
|
| 143 |
template<class... Args> constexpr T& emplace(Args&&...);
|
| 144 |
template<class U, class... Args> constexpr T& emplace(initializer_list<U>, Args&&...);
|
| 145 |
|
| 146 |
// [optional.swap], swap
|
| 147 |
constexpr void swap(optional&) noexcept(see below);
|
| 148 |
|
| 149 |
+
// [optional.iterators], iterator support
|
| 150 |
+
constexpr iterator begin() noexcept;
|
| 151 |
+
constexpr const_iterator begin() const noexcept;
|
| 152 |
+
constexpr iterator end() noexcept;
|
| 153 |
+
constexpr const_iterator end() const noexcept;
|
| 154 |
+
|
| 155 |
// [optional.observe], observers
|
| 156 |
constexpr const T* operator->() const noexcept;
|
| 157 |
constexpr T* operator->() noexcept;
|
| 158 |
constexpr const T& operator*() const & noexcept;
|
| 159 |
constexpr T& operator*() & noexcept;
|
| 160 |
constexpr T&& operator*() && noexcept;
|
| 161 |
constexpr const T&& operator*() const && noexcept;
|
| 162 |
constexpr explicit operator bool() const noexcept;
|
| 163 |
constexpr bool has_value() const noexcept;
|
| 164 |
+
constexpr const T& value() const &; // freestanding-deleted
|
| 165 |
+
constexpr T& value() &; // freestanding-deleted
|
| 166 |
+
constexpr T&& value() &&; // freestanding-deleted
|
| 167 |
+
constexpr const T&& value() const &&; // freestanding-deleted
|
| 168 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&&) const &;
|
| 169 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&&) &&;
|
| 170 |
|
| 171 |
// [optional.monadic], monadic operations
|
| 172 |
template<class F> constexpr auto and_then(F&& f) &;
|
| 173 |
template<class F> constexpr auto and_then(F&& f) &&;
|
| 174 |
template<class F> constexpr auto and_then(F&& f) const &;
|
|
|
|
| 190 |
template<class T>
|
| 191 |
optional(T) -> optional<T>;
|
| 192 |
}
|
| 193 |
```
|
| 194 |
|
| 195 |
+
An object of type `optional<T>` at any given time either contains a
|
| 196 |
+
value or does not contain a value. When an object of type `optional<T>`
|
| 197 |
+
*contains a value*, it means that an object of type `T`, referred to as
|
| 198 |
+
the optional object’s *contained value*, is nested within
|
| 199 |
+
[[intro.object]] the optional object. When an object of type
|
| 200 |
+
`optional<T>` is contextually converted to `bool`, the conversion
|
| 201 |
+
returns `true` if the object contains a value; otherwise the conversion
|
| 202 |
+
returns `false`.
|
|
|
|
| 203 |
|
| 204 |
+
When an object of type `optional<T>` contains a value, member `val`
|
| 205 |
+
points to the contained value.
|
| 206 |
|
| 207 |
+
A type `X` is a *valid contained type* for `optional` if `X` is an
|
| 208 |
+
lvalue reference type or a complete non-array object type, and
|
| 209 |
+
`remove_cvref_t<X>` is a type other than `in_place_t` or `nullopt_t`. If
|
| 210 |
+
a specialization of `optional` is instantiated with a type `T` that is
|
| 211 |
+
not a valid contained type for `optional`, the program is ill-formed. If
|
| 212 |
+
`T` is an object type, `T` shall meet the *Cpp17Destructible*
|
| 213 |
+
requirements ([[cpp17.destructible]]).
|
| 214 |
|
| 215 |
#### Constructors <a id="optional.ctor">[[optional.ctor]]</a>
|
| 216 |
|
| 217 |
The exposition-only variable template *`converts-from-any-cvref`* is
|
| 218 |
used by some constructors for `optional`.
|
|
|
|
| 257 |
```
|
| 258 |
|
| 259 |
*Constraints:* `is_move_constructible_v<T>` is `true`.
|
| 260 |
|
| 261 |
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 262 |
+
contained value with `*std::move(rhs)`. `rhs.has_value()` is unchanged.
|
| 263 |
|
| 264 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 265 |
|
| 266 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 267 |
|
|
|
|
| 303 |
|
| 304 |
*Remarks:* If `T`’s constructor selected for the initialization is a
|
| 305 |
constexpr constructor, this constructor is a constexpr constructor.
|
| 306 |
|
| 307 |
``` cpp
|
| 308 |
+
template<class U = remove_cv_t<T>> constexpr explicit(see below) optional(U&& v);
|
| 309 |
```
|
| 310 |
|
| 311 |
*Constraints:*
|
| 312 |
|
| 313 |
- `is_constructible_v<T, U>` is `true`,
|
|
|
|
| 363 |
- `is_constructible_v<T, U>` is `true`, and
|
| 364 |
- if `T` is not cv `bool`, *`converts-from-any-cvref`*`<T, optional<U>>`
|
| 365 |
is `false`.
|
| 366 |
|
| 367 |
*Effects:* If `rhs` contains a value, direct-non-list-initializes the
|
| 368 |
+
contained value with `*std::move(rhs)`. `rhs.has_value()` is unchanged.
|
| 369 |
|
| 370 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 371 |
|
| 372 |
*Throws:* Any exception thrown by the selected constructor of `T`.
|
| 373 |
|
|
|
|
| 447 |
|
| 448 |
**Table: `optional::operator=(optional&&)` effects** <a id="optional.assign.move">[optional.assign.move]</a>
|
| 449 |
|
| 450 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 451 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 452 |
+
| `rhs` contains a value | assigns `*std::move(rhs)` to the contained value | direct-non-list-initializes the contained value with `*std::move(rhs)` |
|
| 453 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 454 |
|
| 455 |
|
| 456 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 457 |
|
|
|
|
| 473 |
`is_trivially_move_constructible_v<T> &&`
|
| 474 |
`is_trivially_move_assignable_v<T> &&` `is_trivially_destructible_v<T>`
|
| 475 |
is `true`, this assignment operator is trivial.
|
| 476 |
|
| 477 |
``` cpp
|
| 478 |
+
template<class U = remove_cv_t<T>> constexpr optional& operator=(U&& v);
|
| 479 |
```
|
| 480 |
|
| 481 |
+
*Constraints:*
|
| 482 |
+
|
| 483 |
+
- `is_same_v<remove_cvref_t<U>, optional>` is `false`,
|
| 484 |
+
- `conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>>` is `false`,
|
| 485 |
+
- `is_constructible_v<T, U>` is `true`, and
|
| 486 |
+
- `is_assignable_v<T&, U>` is `true`.
|
| 487 |
|
| 488 |
*Effects:* If `*this` contains a value, assigns `std::forward<U>(v)` to
|
| 489 |
the contained value; otherwise direct-non-list-initializes the contained
|
| 490 |
value with `std::forward<U>(v)`.
|
| 491 |
|
|
|
|
| 555 |
|
| 556 |
**Table: `optional::operator=(optional<U>&&)` effects** <a id="optional.assign.move.templ">[optional.assign.move.templ]</a>
|
| 557 |
|
| 558 |
| | `*this` contains a value | `*this` does not contain a value |
|
| 559 |
| ------------------------------ | ------------------------------------------------------ | ---------------------------------------------------------------------- |
|
| 560 |
+
| `rhs` contains a value | assigns `*std::move(rhs)` to the contained value | direct-non-list-initializes the contained value with `*std::move(rhs)` |
|
| 561 |
| `rhs` does not contain a value | destroys the contained value by calling `val->T::~T()` | no effect |
|
| 562 |
|
| 563 |
|
| 564 |
*Ensures:* `rhs.has_value() == this->has_value()`.
|
| 565 |
|
|
|
|
| 648 |
`*val` and `*rhs.val` is determined by the exception safety guarantee of
|
| 649 |
`swap` for lvalues of `T`. If an exception is thrown during the call to
|
| 650 |
`T`’s move constructor, the state of `*val` and `*rhs.val` is determined
|
| 651 |
by the exception safety guarantee of `T`’s move constructor.
|
| 652 |
|
| 653 |
+
#### Iterator support <a id="optional.iterators">[[optional.iterators]]</a>
|
| 654 |
+
|
| 655 |
+
``` cpp
|
| 656 |
+
using iterator = implementation-defined;
|
| 657 |
+
using const_iterator = implementation-defined;
|
| 658 |
+
```
|
| 659 |
+
|
| 660 |
+
These types model `contiguous_iterator` [[iterator.concept.contiguous]],
|
| 661 |
+
meet the *Cpp17RandomAccessIterator*
|
| 662 |
+
requirements [[random.access.iterators]], and meet the requirements for
|
| 663 |
+
constexpr iterators [[iterator.requirements.general]], with value type
|
| 664 |
+
`remove_cv_t<T>`. The reference type is `T&` for `iterator` and
|
| 665 |
+
`const T&` for `const_iterator`.
|
| 666 |
+
|
| 667 |
+
All requirements on container iterators [[container.reqmts]] apply to
|
| 668 |
+
`optional::iterator` and `optional::const_iterator` as well.
|
| 669 |
+
|
| 670 |
+
Any operation that initializes or destroys the contained value of an
|
| 671 |
+
optional object invalidates all iterators into that object.
|
| 672 |
+
|
| 673 |
+
``` cpp
|
| 674 |
+
constexpr iterator begin() noexcept;
|
| 675 |
+
constexpr const_iterator begin() const noexcept;
|
| 676 |
+
```
|
| 677 |
+
|
| 678 |
+
*Returns:* If `has_value()` is `true`, an iterator referring to the
|
| 679 |
+
contained value. Otherwise, a past-the-end iterator value.
|
| 680 |
+
|
| 681 |
+
``` cpp
|
| 682 |
+
constexpr iterator end() noexcept;
|
| 683 |
+
constexpr const_iterator end() const noexcept;
|
| 684 |
+
```
|
| 685 |
+
|
| 686 |
+
*Returns:* `begin() + has_value()`.
|
| 687 |
+
|
| 688 |
#### Observers <a id="optional.observe">[[optional.observe]]</a>
|
| 689 |
|
| 690 |
``` cpp
|
| 691 |
constexpr const T* operator->() const noexcept;
|
| 692 |
constexpr T* operator->() noexcept;
|
| 693 |
```
|
| 694 |
|
| 695 |
+
`has_value()` is `true`.
|
| 696 |
|
| 697 |
*Returns:* `val`.
|
| 698 |
|
| 699 |
*Remarks:* These functions are constexpr functions.
|
| 700 |
|
| 701 |
``` cpp
|
| 702 |
constexpr const T& operator*() const & noexcept;
|
| 703 |
constexpr T& operator*() & noexcept;
|
| 704 |
```
|
| 705 |
|
| 706 |
+
`has_value()` is `true`.
|
| 707 |
|
| 708 |
*Returns:* `*val`.
|
| 709 |
|
| 710 |
*Remarks:* These functions are constexpr functions.
|
| 711 |
|
| 712 |
``` cpp
|
| 713 |
constexpr T&& operator*() && noexcept;
|
| 714 |
constexpr const T&& operator*() const && noexcept;
|
| 715 |
```
|
| 716 |
|
| 717 |
+
`has_value()` is `true`.
|
| 718 |
|
| 719 |
*Effects:* Equivalent to: `return std::move(*val);`
|
| 720 |
|
| 721 |
``` cpp
|
| 722 |
constexpr explicit operator bool() const noexcept;
|
|
|
|
| 755 |
``` cpp
|
| 756 |
return has_value() ? std::move(*val) : throw bad_optional_access();
|
| 757 |
```
|
| 758 |
|
| 759 |
``` cpp
|
| 760 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) const &;
|
| 761 |
```
|
| 762 |
|
| 763 |
*Mandates:* `is_copy_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 764 |
`true`.
|
| 765 |
|
|
|
|
| 768 |
``` cpp
|
| 769 |
return has_value() ? **this : static_cast<T>(std::forward<U>(v));
|
| 770 |
```
|
| 771 |
|
| 772 |
``` cpp
|
| 773 |
+
template<class U = remove_cv_t<T>> constexpr T value_or(U&& v) &&;
|
| 774 |
```
|
| 775 |
|
| 776 |
*Mandates:* `is_move_constructible_v<T> && is_convertible_v<U&&, T>` is
|
| 777 |
`true`.
|
| 778 |
|
|
|
|
| 787 |
``` cpp
|
| 788 |
template<class F> constexpr auto and_then(F&& f) &;
|
| 789 |
template<class F> constexpr auto and_then(F&& f) const &;
|
| 790 |
```
|
| 791 |
|
| 792 |
+
Let `U` be `invoke_result_t<F, decltype(*`*`val`*`)>`.
|
| 793 |
|
| 794 |
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 795 |
|
| 796 |
*Effects:* Equivalent to:
|
| 797 |
|
| 798 |
``` cpp
|
| 799 |
if (*this) {
|
| 800 |
+
return invoke(std::forward<F>(f), *val);
|
| 801 |
} else {
|
| 802 |
return remove_cvref_t<U>();
|
| 803 |
}
|
| 804 |
```
|
| 805 |
|
| 806 |
``` cpp
|
| 807 |
template<class F> constexpr auto and_then(F&& f) &&;
|
| 808 |
template<class F> constexpr auto and_then(F&& f) const &&;
|
| 809 |
```
|
| 810 |
|
| 811 |
+
Let `U` be `invoke_result_t<F, decltype(std::move(*`*`val`*`))>`.
|
| 812 |
|
| 813 |
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 814 |
|
| 815 |
*Effects:* Equivalent to:
|
| 816 |
|
| 817 |
``` cpp
|
| 818 |
if (*this) {
|
| 819 |
+
return invoke(std::forward<F>(f), std::move(*val));
|
| 820 |
} else {
|
| 821 |
return remove_cvref_t<U>();
|
| 822 |
}
|
| 823 |
```
|
| 824 |
|
| 825 |
``` cpp
|
| 826 |
template<class F> constexpr auto transform(F&& f) &;
|
| 827 |
template<class F> constexpr auto transform(F&& f) const &;
|
| 828 |
```
|
| 829 |
|
| 830 |
+
Let `U` be `remove_cv_t<invoke_result_t<F, decltype(*`*`val`*`)>>`.
|
| 831 |
|
| 832 |
+
*Mandates:* `U` is a valid contained type for `optional`. The
|
| 833 |
+
declaration
|
| 834 |
|
| 835 |
``` cpp
|
| 836 |
+
U u(invoke(std::forward<F>(f), *val));
|
| 837 |
```
|
| 838 |
|
| 839 |
is well-formed for some invented variable `u`.
|
| 840 |
|
| 841 |
[*Note 1*: There is no requirement that `U` is
|
| 842 |
movable [[dcl.init.general]]. — *end note*]
|
| 843 |
|
| 844 |
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 845 |
contained value is direct-non-list-initialized with
|
| 846 |
+
`invoke(std::forward<F>(f), *`*`val`*`)`; otherwise, `optional<U>()`.
|
| 847 |
|
| 848 |
``` cpp
|
| 849 |
template<class F> constexpr auto transform(F&& f) &&;
|
| 850 |
template<class F> constexpr auto transform(F&& f) const &&;
|
| 851 |
```
|
| 852 |
|
| 853 |
Let `U` be
|
| 854 |
+
`remove_cv_t<invoke_result_t<F, decltype(std::move(*`*`val`*`))>>`.
|
| 855 |
|
| 856 |
+
*Mandates:* `U` is a valid contained type for `optional`. The
|
| 857 |
+
declaration
|
| 858 |
|
| 859 |
``` cpp
|
| 860 |
+
U u(invoke(std::forward<F>(f), std::move(*val)));
|
| 861 |
```
|
| 862 |
|
| 863 |
is well-formed for some invented variable `u`.
|
| 864 |
|
| 865 |
[*Note 2*: There is no requirement that `U` is
|
| 866 |
movable [[dcl.init.general]]. — *end note*]
|
| 867 |
|
| 868 |
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 869 |
contained value is direct-non-list-initialized with
|
| 870 |
+
`invoke(std::forward<F>(f), std::move(*`*`val`*`))`; otherwise,
|
| 871 |
`optional<U>()`.
|
| 872 |
|
| 873 |
``` cpp
|
| 874 |
template<class F> constexpr optional or_else(F&& f) const &;
|
| 875 |
```
|
| 876 |
|
| 877 |
+
*Constraints:* `F` models `invocable` and `T` models
|
| 878 |
`copy_constructible`.
|
| 879 |
|
| 880 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 881 |
`true`.
|
| 882 |
|
|
|
|
| 892 |
|
| 893 |
``` cpp
|
| 894 |
template<class F> constexpr optional or_else(F&& f) &&;
|
| 895 |
```
|
| 896 |
|
| 897 |
+
*Constraints:* `F` models `invocable` and `T` models
|
| 898 |
`move_constructible`.
|
| 899 |
|
| 900 |
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 901 |
`true`.
|
| 902 |
|
|
|
|
| 919 |
*Effects:* If `*this` contains a value, calls `val->T::T̃()` to destroy
|
| 920 |
the contained value; otherwise no effect.
|
| 921 |
|
| 922 |
*Ensures:* `*this` does not contain a value.
|
| 923 |
|
| 924 |
+
### Partial specialization of `optional` for reference types <a id="optional.optional.ref">[[optional.optional.ref]]</a>
|
| 925 |
+
|
| 926 |
+
#### General <a id="optional.optional.ref.general">[[optional.optional.ref.general]]</a>
|
| 927 |
+
|
| 928 |
+
``` cpp
|
| 929 |
+
namespace std {
|
| 930 |
+
template<class T>
|
| 931 |
+
class optional<T&> {
|
| 932 |
+
public:
|
| 933 |
+
using value_type = T;
|
| 934 |
+
using iterator = implementation-defined; // see~[optional.ref.iterators]
|
| 935 |
+
|
| 936 |
+
public:
|
| 937 |
+
// [optional.ref.ctor], constructors
|
| 938 |
+
constexpr optional() noexcept = default;
|
| 939 |
+
constexpr optional(nullopt_t) noexcept : optional() {}
|
| 940 |
+
constexpr optional(const optional& rhs) noexcept = default;
|
| 941 |
+
|
| 942 |
+
template<class Arg>
|
| 943 |
+
constexpr explicit optional(in_place_t, Arg&& arg);
|
| 944 |
+
template<class U>
|
| 945 |
+
constexpr explicit(see below) optional(U&& u) noexcept(see below);
|
| 946 |
+
template<class U>
|
| 947 |
+
constexpr explicit(see below) optional(optional<U>& rhs) noexcept(see below);
|
| 948 |
+
template<class U>
|
| 949 |
+
constexpr explicit(see below) optional(const optional<U>& rhs) noexcept(see below);
|
| 950 |
+
template<class U>
|
| 951 |
+
constexpr explicit(see below) optional(optional<U>&& rhs) noexcept(see below);
|
| 952 |
+
template<class U>
|
| 953 |
+
constexpr explicit(see below) optional(const optional<U>&& rhs) noexcept(see below);
|
| 954 |
+
|
| 955 |
+
constexpr ~optional() = default;
|
| 956 |
+
|
| 957 |
+
// [optional.ref.assign], assignment
|
| 958 |
+
constexpr optional& operator=(nullopt_t) noexcept;
|
| 959 |
+
constexpr optional& operator=(const optional& rhs) noexcept = default;
|
| 960 |
+
|
| 961 |
+
template<class U> constexpr T& emplace(U&& u) noexcept(see below);
|
| 962 |
+
|
| 963 |
+
// [optional.ref.swap], swap
|
| 964 |
+
constexpr void swap(optional& rhs) noexcept;
|
| 965 |
+
|
| 966 |
+
// [optional.ref.iterators], iterator support
|
| 967 |
+
constexpr iterator begin() const noexcept;
|
| 968 |
+
constexpr iterator end() const noexcept;
|
| 969 |
+
|
| 970 |
+
// [optional.ref.observe], observers
|
| 971 |
+
constexpr T* operator->() const noexcept;
|
| 972 |
+
constexpr T& operator*() const noexcept;
|
| 973 |
+
constexpr explicit operator bool() const noexcept;
|
| 974 |
+
constexpr bool has_value() const noexcept;
|
| 975 |
+
constexpr T& value() const; // freestanding-deleted
|
| 976 |
+
template<class U = remove_cv_t<T>>
|
| 977 |
+
constexpr remove_cv_t<T> value_or(U&& u) const;
|
| 978 |
+
|
| 979 |
+
// [optional.ref.monadic], monadic operations
|
| 980 |
+
template<class F> constexpr auto and_then(F&& f) const;
|
| 981 |
+
template<class F> constexpr optional<invoke_result_t<F, T&>> transform(F&& f) const;
|
| 982 |
+
template<class F> constexpr optional or_else(F&& f) const;
|
| 983 |
+
|
| 984 |
+
// [optional.ref.mod], modifiers
|
| 985 |
+
constexpr void reset() noexcept;
|
| 986 |
+
|
| 987 |
+
private:
|
| 988 |
+
T* val = nullptr; // exposition only
|
| 989 |
+
|
| 990 |
+
// [optional.ref.expos], exposition only helper functions
|
| 991 |
+
template<class U>
|
| 992 |
+
constexpr void convert-ref-init-val(U&& u); // exposition only
|
| 993 |
+
};
|
| 994 |
+
}
|
| 995 |
+
```
|
| 996 |
+
|
| 997 |
+
An object of type `optional<T&>` *contains a value* if and only if
|
| 998 |
+
`val != nullptr` is `true`. When an `optional<T&>` contains a value, the
|
| 999 |
+
*contained value* is a reference to `*val`.
|
| 1000 |
+
|
| 1001 |
+
#### Constructors <a id="optional.ref.ctor">[[optional.ref.ctor]]</a>
|
| 1002 |
+
|
| 1003 |
+
``` cpp
|
| 1004 |
+
template<class Arg>
|
| 1005 |
+
constexpr explicit optional(in_place_t, Arg&& arg);
|
| 1006 |
+
```
|
| 1007 |
+
|
| 1008 |
+
*Constraints:*
|
| 1009 |
+
|
| 1010 |
+
- `is_constructible_v<T&, Arg>` is `true`, and
|
| 1011 |
+
- `reference_constructs_from_temporary_v<T&, Arg>` is `false`.
|
| 1012 |
+
|
| 1013 |
+
*Effects:* Equivalent to:
|
| 1014 |
+
*`convert-ref-init-val`*`(std::forward<Arg>(arg))`.
|
| 1015 |
+
|
| 1016 |
+
*Ensures:* `*this` contains a value.
|
| 1017 |
+
|
| 1018 |
+
``` cpp
|
| 1019 |
+
template<class U>
|
| 1020 |
+
constexpr explicit(!is_convertible_v<U, T&>)
|
| 1021 |
+
optional(U&& u) noexcept(is_nothrow_constructible_v<T&, U>);
|
| 1022 |
+
```
|
| 1023 |
+
|
| 1024 |
+
*Constraints:*
|
| 1025 |
+
|
| 1026 |
+
- `is_same_v<remove_cvref_t<U>, optional>` is `false`,
|
| 1027 |
+
- `is_same_v<remove_cvref_t<U>, in_place_t>` is `false`, and
|
| 1028 |
+
- `is_constructible_v<T&, U>` is `true`.
|
| 1029 |
+
|
| 1030 |
+
*Effects:* Equivalent to:
|
| 1031 |
+
*`convert-ref-init-val`*`(std::forward<U>(u))`.
|
| 1032 |
+
|
| 1033 |
+
*Ensures:* `*this` contains a value.
|
| 1034 |
+
|
| 1035 |
+
*Remarks:* This constructor is defined as deleted if
|
| 1036 |
+
|
| 1037 |
+
``` cpp
|
| 1038 |
+
reference_constructs_from_temporary_v<T&, U>
|
| 1039 |
+
```
|
| 1040 |
+
|
| 1041 |
+
is `true`.
|
| 1042 |
+
|
| 1043 |
+
``` cpp
|
| 1044 |
+
template<class U>
|
| 1045 |
+
constexpr explicit(!is_convertible_v<U&, T&>)
|
| 1046 |
+
optional(optional<U>& rhs) noexcept(is_nothrow_constructible_v<T&, U&>);
|
| 1047 |
+
```
|
| 1048 |
+
|
| 1049 |
+
*Constraints:*
|
| 1050 |
+
|
| 1051 |
+
- `is_same_v<remove_cv_t<T>, optional<U>>` is `false`,
|
| 1052 |
+
- `is_same_v<T&, U>` is `false`, and
|
| 1053 |
+
- `is_constructible_v<T&, U&>` is `true`.
|
| 1054 |
+
|
| 1055 |
+
*Effects:* Equivalent to:
|
| 1056 |
+
|
| 1057 |
+
``` cpp
|
| 1058 |
+
if (rhs.has_value()) convert-ref-init-val(*rhs);
|
| 1059 |
+
```
|
| 1060 |
+
|
| 1061 |
+
*Remarks:* This constructor is defined as deleted if
|
| 1062 |
+
|
| 1063 |
+
``` cpp
|
| 1064 |
+
reference_constructs_from_temporary_v<T&, U&>
|
| 1065 |
+
```
|
| 1066 |
+
|
| 1067 |
+
is `true`.
|
| 1068 |
+
|
| 1069 |
+
``` cpp
|
| 1070 |
+
template<class U>
|
| 1071 |
+
constexpr explicit(!is_convertible_v<const U&, T&>)
|
| 1072 |
+
optional(const optional<U>& rhs) noexcept(is_nothrow_constructible_v<T&, const U&>);
|
| 1073 |
+
```
|
| 1074 |
+
|
| 1075 |
+
*Constraints:*
|
| 1076 |
+
|
| 1077 |
+
- `is_same_v<remove_cv_t<T>, optional<U>>` is `false`,
|
| 1078 |
+
- `is_same_v<T&, U>` is `false`, and
|
| 1079 |
+
- `is_constructible_v<T&, const U&>` is `true`.
|
| 1080 |
+
|
| 1081 |
+
*Effects:* Equivalent to:
|
| 1082 |
+
|
| 1083 |
+
``` cpp
|
| 1084 |
+
if (rhs.has_value()) convert-ref-init-val(*rhs);
|
| 1085 |
+
```
|
| 1086 |
+
|
| 1087 |
+
*Remarks:* This constructor is defined as deleted if
|
| 1088 |
+
|
| 1089 |
+
``` cpp
|
| 1090 |
+
reference_constructs_from_temporary_v<T&, const U&>
|
| 1091 |
+
```
|
| 1092 |
+
|
| 1093 |
+
is `true`.
|
| 1094 |
+
|
| 1095 |
+
``` cpp
|
| 1096 |
+
template<class U>
|
| 1097 |
+
constexpr explicit(!is_convertible_v<U, T&>)
|
| 1098 |
+
optional(optional<U>&& rhs) noexcept(is_nothrow_constructible_v<T&, U>);
|
| 1099 |
+
```
|
| 1100 |
+
|
| 1101 |
+
*Constraints:*
|
| 1102 |
+
|
| 1103 |
+
- `is_same_v<remove_cv_t<T>, optional<U>>` is `false`,
|
| 1104 |
+
- `is_same_v<T&, U>` is `false`, and
|
| 1105 |
+
- `is_constructible_v<T&, U>` is `true`.
|
| 1106 |
+
|
| 1107 |
+
*Effects:* Equivalent to:
|
| 1108 |
+
|
| 1109 |
+
``` cpp
|
| 1110 |
+
if (rhs.has_value()) convert-ref-init-val(*std::move(rhs));
|
| 1111 |
+
```
|
| 1112 |
+
|
| 1113 |
+
*Remarks:* This constructor is defined as deleted if
|
| 1114 |
+
|
| 1115 |
+
``` cpp
|
| 1116 |
+
reference_constructs_from_temporary_v<T&, U>
|
| 1117 |
+
```
|
| 1118 |
+
|
| 1119 |
+
is `true`.
|
| 1120 |
+
|
| 1121 |
+
``` cpp
|
| 1122 |
+
template<class U>
|
| 1123 |
+
constexpr explicit(!is_convertible_v<const U, T&>)
|
| 1124 |
+
optional(const optional<U>&& rhs) noexcept(is_nothrow_constructible_v<T&, const U>);
|
| 1125 |
+
```
|
| 1126 |
+
|
| 1127 |
+
*Constraints:*
|
| 1128 |
+
|
| 1129 |
+
- `is_same_v<remove_cv_t<T>, optional<U>>` is `false`,
|
| 1130 |
+
- `is_same_v<T&, U>` is `false`, and
|
| 1131 |
+
- `is_constructible_v<T&, const U>` is `true`.
|
| 1132 |
+
|
| 1133 |
+
*Effects:* Equivalent to:
|
| 1134 |
+
|
| 1135 |
+
``` cpp
|
| 1136 |
+
if (rhs.has_value()) convert-ref-init-val(*std::move(rhs));
|
| 1137 |
+
```
|
| 1138 |
+
|
| 1139 |
+
*Remarks:* This constructor is defined as deleted if
|
| 1140 |
+
|
| 1141 |
+
``` cpp
|
| 1142 |
+
reference_constructs_from_temporary_v<T&, const U>
|
| 1143 |
+
```
|
| 1144 |
+
|
| 1145 |
+
is `true`.
|
| 1146 |
+
|
| 1147 |
+
#### Assignment <a id="optional.ref.assign">[[optional.ref.assign]]</a>
|
| 1148 |
+
|
| 1149 |
+
``` cpp
|
| 1150 |
+
constexpr optional& operator=(nullopt_t) noexcept;
|
| 1151 |
+
```
|
| 1152 |
+
|
| 1153 |
+
*Effects:* Assigns `nullptr` to *val*.
|
| 1154 |
+
|
| 1155 |
+
*Ensures:* `*this` does not contain a value.
|
| 1156 |
+
|
| 1157 |
+
*Returns:* `*this`.
|
| 1158 |
+
|
| 1159 |
+
``` cpp
|
| 1160 |
+
template<class U>
|
| 1161 |
+
constexpr T& emplace(U&& u) noexcept(is_nothrow_constructible_v<T&, U>);
|
| 1162 |
+
```
|
| 1163 |
+
|
| 1164 |
+
*Constraints:*
|
| 1165 |
+
|
| 1166 |
+
- `is_constructible_v<T&, U>` is `true`, and
|
| 1167 |
+
- `reference_constructs_from_temporary_v<T&, U>` is `false`.
|
| 1168 |
+
|
| 1169 |
+
*Effects:* Equivalent to:
|
| 1170 |
+
*`convert-ref-init-val`*`(std::forward<U>(u))`.
|
| 1171 |
+
|
| 1172 |
+
*Returns:* `*`*`val`*.
|
| 1173 |
+
|
| 1174 |
+
#### Swap <a id="optional.ref.swap">[[optional.ref.swap]]</a>
|
| 1175 |
+
|
| 1176 |
+
``` cpp
|
| 1177 |
+
constexpr void swap(optional& rhs) noexcept;
|
| 1178 |
+
```
|
| 1179 |
+
|
| 1180 |
+
*Effects:* Equivalent to: `swap(`*`val`*`, rhs.`*`val`*`)`.
|
| 1181 |
+
|
| 1182 |
+
#### Iterator support <a id="optional.ref.iterators">[[optional.ref.iterators]]</a>
|
| 1183 |
+
|
| 1184 |
+
``` cpp
|
| 1185 |
+
using iterator = implementation-defined;
|
| 1186 |
+
```
|
| 1187 |
+
|
| 1188 |
+
This type models `contiguous_iterator` [[iterator.concept.contiguous]],
|
| 1189 |
+
meets the *Cpp17RandomAccessIterator*
|
| 1190 |
+
requirements [[random.access.iterators]], and meets the requirements for
|
| 1191 |
+
constexpr iterators [[iterator.requirements.general]], with value type
|
| 1192 |
+
`remove_cv_t<T>`. The reference type is `T&` for `iterator`.
|
| 1193 |
+
|
| 1194 |
+
All requirements on container iterators [[container.reqmts]] apply to
|
| 1195 |
+
`optional::iterator`.
|
| 1196 |
+
|
| 1197 |
+
``` cpp
|
| 1198 |
+
constexpr iterator begin() const noexcept;
|
| 1199 |
+
```
|
| 1200 |
+
|
| 1201 |
+
*Returns:* If `has_value()` is `true`, an iterator referring to
|
| 1202 |
+
`*`*`val`*. Otherwise, a past-the-end iterator value.
|
| 1203 |
+
|
| 1204 |
+
``` cpp
|
| 1205 |
+
constexpr iterator end() const noexcept;
|
| 1206 |
+
```
|
| 1207 |
+
|
| 1208 |
+
*Returns:* `begin() + has_value()`.
|
| 1209 |
+
|
| 1210 |
+
#### Observers <a id="optional.ref.observe">[[optional.ref.observe]]</a>
|
| 1211 |
+
|
| 1212 |
+
``` cpp
|
| 1213 |
+
constexpr T* operator->() const noexcept;
|
| 1214 |
+
```
|
| 1215 |
+
|
| 1216 |
+
`has_value()` is `true`.
|
| 1217 |
+
|
| 1218 |
+
*Returns:* *val*.
|
| 1219 |
+
|
| 1220 |
+
``` cpp
|
| 1221 |
+
constexpr T& operator*() const noexcept;
|
| 1222 |
+
```
|
| 1223 |
+
|
| 1224 |
+
`has_value()` is `true`.
|
| 1225 |
+
|
| 1226 |
+
*Returns:* `*`*`val`*.
|
| 1227 |
+
|
| 1228 |
+
``` cpp
|
| 1229 |
+
constexpr explicit operator bool() const noexcept;
|
| 1230 |
+
```
|
| 1231 |
+
|
| 1232 |
+
*Returns:* *`val`*` != nullptr`.
|
| 1233 |
+
|
| 1234 |
+
``` cpp
|
| 1235 |
+
constexpr bool has_value() const noexcept;
|
| 1236 |
+
```
|
| 1237 |
+
|
| 1238 |
+
*Returns:* *`val`*` != nullptr`.
|
| 1239 |
+
|
| 1240 |
+
``` cpp
|
| 1241 |
+
constexpr T& value() const;
|
| 1242 |
+
```
|
| 1243 |
+
|
| 1244 |
+
*Effects:* Equivalent to:
|
| 1245 |
+
|
| 1246 |
+
``` cpp
|
| 1247 |
+
return has_value() ? *val : throw bad_optional_access();
|
| 1248 |
+
```
|
| 1249 |
+
|
| 1250 |
+
``` cpp
|
| 1251 |
+
template<class U = remove_cv_t<T>> constexpr remove_cv_t<T> value_or(U&& u) const;
|
| 1252 |
+
```
|
| 1253 |
+
|
| 1254 |
+
Let `X` be `remove_cv_t<T>`.
|
| 1255 |
+
|
| 1256 |
+
*Mandates:* `is_constructible_v<X, T&> && is_convertible_v<U, X>` is
|
| 1257 |
+
`true`.
|
| 1258 |
+
|
| 1259 |
+
*Effects:* Equivalent to:
|
| 1260 |
+
|
| 1261 |
+
``` cpp
|
| 1262 |
+
return has_value() ? *val : static_cast<X>(std::forward<U>(u));
|
| 1263 |
+
```
|
| 1264 |
+
|
| 1265 |
+
#### Monadic operations <a id="optional.ref.monadic">[[optional.ref.monadic]]</a>
|
| 1266 |
+
|
| 1267 |
+
``` cpp
|
| 1268 |
+
template<class F> constexpr auto and_then(F&& f) const;
|
| 1269 |
+
```
|
| 1270 |
+
|
| 1271 |
+
Let `U` be `invoke_result_t<F, T&>`.
|
| 1272 |
+
|
| 1273 |
+
*Mandates:* `remove_cvref_t<U>` is a specialization of `optional`.
|
| 1274 |
+
|
| 1275 |
+
*Effects:* Equivalent to:
|
| 1276 |
+
|
| 1277 |
+
``` cpp
|
| 1278 |
+
if (has_value()) {
|
| 1279 |
+
return invoke(std::forward<F>(f), *val);
|
| 1280 |
+
} else {
|
| 1281 |
+
return remove_cvref_t<U>();
|
| 1282 |
+
}
|
| 1283 |
+
```
|
| 1284 |
+
|
| 1285 |
+
``` cpp
|
| 1286 |
+
template<class F>
|
| 1287 |
+
constexpr optional<remove_cv_t<invoke_result_t<F, T&>>> transform(F&& f) const;
|
| 1288 |
+
```
|
| 1289 |
+
|
| 1290 |
+
Let `U` be `remove_cv_t<invoke_result_t<F, T&>>`.
|
| 1291 |
+
|
| 1292 |
+
*Mandates:* The declaration
|
| 1293 |
+
|
| 1294 |
+
``` cpp
|
| 1295 |
+
U u(invoke(std::forward<F>(f), *val));
|
| 1296 |
+
```
|
| 1297 |
+
|
| 1298 |
+
is well-formed for some invented variable `u`.
|
| 1299 |
+
|
| 1300 |
+
[*Note 1*: There is no requirement that `U` is
|
| 1301 |
+
movable [[dcl.init.general]]. — *end note*]
|
| 1302 |
+
|
| 1303 |
+
*Returns:* If `*this` contains a value, an `optional<U>` object whose
|
| 1304 |
+
contained value is direct-non-list-initialized with
|
| 1305 |
+
`invoke(std::forward<F>(f), *`*`val`*`)`; otherwise, `optional<U>()`.
|
| 1306 |
+
|
| 1307 |
+
``` cpp
|
| 1308 |
+
template<class F> constexpr optional or_else(F&& f) const;
|
| 1309 |
+
```
|
| 1310 |
+
|
| 1311 |
+
*Constraints:* `F` models `invocable`.
|
| 1312 |
+
|
| 1313 |
+
*Mandates:* `is_same_v<remove_cvref_t<invoke_result_t<F>>, optional>` is
|
| 1314 |
+
`true`.
|
| 1315 |
+
|
| 1316 |
+
*Effects:* Equivalent to:
|
| 1317 |
+
|
| 1318 |
+
``` cpp
|
| 1319 |
+
if (has_value()) {
|
| 1320 |
+
return *val;
|
| 1321 |
+
} else {
|
| 1322 |
+
return std::forward<F>(f)();
|
| 1323 |
+
}
|
| 1324 |
+
```
|
| 1325 |
+
|
| 1326 |
+
#### Modifiers <a id="optional.ref.mod">[[optional.ref.mod]]</a>
|
| 1327 |
+
|
| 1328 |
+
``` cpp
|
| 1329 |
+
constexpr void reset() noexcept;
|
| 1330 |
+
```
|
| 1331 |
+
|
| 1332 |
+
*Effects:* Assigns `nullptr` to *val*.
|
| 1333 |
+
|
| 1334 |
+
*Ensures:* `*this` does not contain a value.
|
| 1335 |
+
|
| 1336 |
+
#### Exposition only helper functions <a id="optional.ref.expos">[[optional.ref.expos]]</a>
|
| 1337 |
+
|
| 1338 |
+
``` cpp
|
| 1339 |
+
template<class U>
|
| 1340 |
+
constexpr void convert-ref-init-val(U&& u); // exposition only
|
| 1341 |
+
```
|
| 1342 |
+
|
| 1343 |
+
*Effects:* Creates a variable `r` as if by `T& r(std::forward<U>(u));`
|
| 1344 |
+
and then initializes *val* with `addressof(r)`.
|
| 1345 |
+
|
| 1346 |
### No-value state indicator <a id="optional.nullopt">[[optional.nullopt]]</a>
|
| 1347 |
|
| 1348 |
``` cpp
|
| 1349 |
struct nullopt_t{see below};
|
| 1350 |
inline constexpr nullopt_t nullopt(unspecified);
|
|
|
|
| 1364 |
``` cpp
|
| 1365 |
namespace std {
|
| 1366 |
class bad_optional_access : public exception {
|
| 1367 |
public:
|
| 1368 |
// see [exception] for the specification of the special member functions
|
| 1369 |
+
constexpr const char* what() const noexcept override;
|
| 1370 |
};
|
| 1371 |
}
|
| 1372 |
```
|
| 1373 |
|
| 1374 |
The class `bad_optional_access` defines the type of objects thrown as
|
| 1375 |
exceptions to report the situation where an attempt is made to access
|
| 1376 |
the value of an optional object that does not contain a value.
|
| 1377 |
|
| 1378 |
``` cpp
|
| 1379 |
+
constexpr const char* what() const noexcept override;
|
| 1380 |
```
|
| 1381 |
|
| 1382 |
+
*Returns:* An *implementation-defined* NTBS, which during constant
|
| 1383 |
+
evaluation is encoded with the ordinary literal encoding [[lex.ccon]].
|
| 1384 |
|
| 1385 |
### Relational operators <a id="optional.relops">[[optional.relops]]</a>
|
| 1386 |
|
| 1387 |
``` cpp
|
| 1388 |
template<class T, class U> constexpr bool operator==(const optional<T>& x, const optional<U>& y);
|
| 1389 |
```
|
| 1390 |
|
| 1391 |
+
*Constraints:* The expression `*x == *y` is well-formed and its result
|
| 1392 |
+
is convertible to `bool`.
|
| 1393 |
|
| 1394 |
[*Note 1*: `T` need not be *Cpp17EqualityComparable*. — *end note*]
|
| 1395 |
|
| 1396 |
*Returns:* If `x.has_value() != y.has_value()`, `false`; otherwise if
|
| 1397 |
`x.has_value() == false`, `true`; otherwise `*x == *y`.
|
|
|
|
| 1401 |
|
| 1402 |
``` cpp
|
| 1403 |
template<class T, class U> constexpr bool operator!=(const optional<T>& x, const optional<U>& y);
|
| 1404 |
```
|
| 1405 |
|
| 1406 |
+
*Constraints:* The expression `*x != *y` is well-formed and its result
|
| 1407 |
+
is convertible to `bool`.
|
| 1408 |
|
| 1409 |
*Returns:* If `x.has_value() != y.has_value()`, `true`; otherwise, if
|
| 1410 |
`x.has_value() == false`, `false`; otherwise `*x != *y`.
|
| 1411 |
|
| 1412 |
*Remarks:* Specializations of this function template for which
|
|
|
|
| 1414 |
|
| 1415 |
``` cpp
|
| 1416 |
template<class T, class U> constexpr bool operator<(const optional<T>& x, const optional<U>& y);
|
| 1417 |
```
|
| 1418 |
|
| 1419 |
+
*Constraints:* `*x < *y` is well-formed and its result is convertible to
|
| 1420 |
`bool`.
|
| 1421 |
|
| 1422 |
*Returns:* If `!y`, `false`; otherwise, if `!x`, `true`; otherwise
|
| 1423 |
`*x < *y`.
|
| 1424 |
|
|
|
|
| 1427 |
|
| 1428 |
``` cpp
|
| 1429 |
template<class T, class U> constexpr bool operator>(const optional<T>& x, const optional<U>& y);
|
| 1430 |
```
|
| 1431 |
|
| 1432 |
+
*Constraints:* The expression `*x > *y` is well-formed and its result is
|
| 1433 |
convertible to `bool`.
|
| 1434 |
|
| 1435 |
*Returns:* If `!x`, `false`; otherwise, if `!y`, `true`; otherwise
|
| 1436 |
`*x > *y`.
|
| 1437 |
|
|
|
|
| 1440 |
|
| 1441 |
``` cpp
|
| 1442 |
template<class T, class U> constexpr bool operator<=(const optional<T>& x, const optional<U>& y);
|
| 1443 |
```
|
| 1444 |
|
| 1445 |
+
*Constraints:* The expression `*x <= *y` is well-formed and its result
|
| 1446 |
+
is convertible to `bool`.
|
| 1447 |
|
| 1448 |
*Returns:* If `!x`, `true`; otherwise, if `!y`, `false`; otherwise
|
| 1449 |
`*x <= *y`.
|
| 1450 |
|
| 1451 |
*Remarks:* Specializations of this function template for which
|
|
|
|
| 1453 |
|
| 1454 |
``` cpp
|
| 1455 |
template<class T, class U> constexpr bool operator>=(const optional<T>& x, const optional<U>& y);
|
| 1456 |
```
|
| 1457 |
|
| 1458 |
+
*Constraints:* The expression `*x >= *y` is well-formed and its result
|
| 1459 |
+
is convertible to `bool`.
|
| 1460 |
|
| 1461 |
*Returns:* If `!y`, `true`; otherwise, if `!x`, `false`; otherwise
|
| 1462 |
`*x >= *y`.
|
| 1463 |
|
| 1464 |
*Remarks:* Specializations of this function template for which
|
|
|
|
| 1494 |
|
| 1495 |
``` cpp
|
| 1496 |
template<class T, class U> constexpr bool operator==(const optional<T>& x, const U& v);
|
| 1497 |
```
|
| 1498 |
|
| 1499 |
+
*Constraints:* `U` is not a specialization of `optional`. The expression
|
| 1500 |
+
`*x == v` is well-formed and its result is convertible to `bool`.
|
| 1501 |
|
| 1502 |
[*Note 1*: `T` need not be *Cpp17EqualityComparable*. — *end note*]
|
| 1503 |
|
| 1504 |
*Effects:* Equivalent to: `return x.has_value() ? *x == v : false;`
|
| 1505 |
|
| 1506 |
``` cpp
|
| 1507 |
template<class T, class U> constexpr bool operator==(const T& v, const optional<U>& x);
|
| 1508 |
```
|
| 1509 |
|
| 1510 |
+
*Constraints:* `T` is not a specialization of `optional`. The expression
|
| 1511 |
+
`v == *x` is well-formed and its result is convertible to `bool`.
|
| 1512 |
|
| 1513 |
*Effects:* Equivalent to: `return x.has_value() ? v == *x : false;`
|
| 1514 |
|
| 1515 |
``` cpp
|
| 1516 |
template<class T, class U> constexpr bool operator!=(const optional<T>& x, const U& v);
|
| 1517 |
```
|
| 1518 |
|
| 1519 |
+
*Constraints:* `U` is not a specialization of `optional`. The expression
|
| 1520 |
+
`*x != v` is well-formed and its result is convertible to `bool`.
|
| 1521 |
|
| 1522 |
*Effects:* Equivalent to: `return x.has_value() ? *x != v : true;`
|
| 1523 |
|
| 1524 |
``` cpp
|
| 1525 |
template<class T, class U> constexpr bool operator!=(const T& v, const optional<U>& x);
|
| 1526 |
```
|
| 1527 |
|
| 1528 |
+
*Constraints:* `T` is not a specialization of `optional`. The expression
|
| 1529 |
+
`v != *x` is well-formed and its result is convertible to `bool`.
|
| 1530 |
|
| 1531 |
*Effects:* Equivalent to: `return x.has_value() ? v != *x : true;`
|
| 1532 |
|
| 1533 |
``` cpp
|
| 1534 |
template<class T, class U> constexpr bool operator<(const optional<T>& x, const U& v);
|
| 1535 |
```
|
| 1536 |
|
| 1537 |
+
*Constraints:* `U` is not a specialization of `optional`. The expression
|
| 1538 |
+
`*x < v` is well-formed and its result is convertible to `bool`.
|
| 1539 |
|
| 1540 |
*Effects:* Equivalent to: `return x.has_value() ? *x < v : true;`
|
| 1541 |
|
| 1542 |
``` cpp
|
| 1543 |
template<class T, class U> constexpr bool operator<(const T& v, const optional<U>& x);
|
| 1544 |
```
|
| 1545 |
|
| 1546 |
+
*Constraints:* `T` is not a specialization of `optional`. The expression
|
| 1547 |
+
`v < *x` is well-formed and its result is convertible to `bool`.
|
| 1548 |
|
| 1549 |
*Effects:* Equivalent to: `return x.has_value() ? v < *x : false;`
|
| 1550 |
|
| 1551 |
``` cpp
|
| 1552 |
template<class T, class U> constexpr bool operator>(const optional<T>& x, const U& v);
|
| 1553 |
```
|
| 1554 |
|
| 1555 |
+
*Constraints:* `U` is not a specialization of `optional`. The expression
|
| 1556 |
+
`*x > v` is well-formed and its result is convertible to `bool`.
|
| 1557 |
|
| 1558 |
*Effects:* Equivalent to: `return x.has_value() ? *x > v : false;`
|
| 1559 |
|
| 1560 |
``` cpp
|
| 1561 |
template<class T, class U> constexpr bool operator>(const T& v, const optional<U>& x);
|
| 1562 |
```
|
| 1563 |
|
| 1564 |
+
*Constraints:* `T` is not a specialization of `optional`. The expression
|
| 1565 |
+
`v > *x` is well-formed and its result is convertible to `bool`.
|
| 1566 |
|
| 1567 |
*Effects:* Equivalent to: `return x.has_value() ? v > *x : true;`
|
| 1568 |
|
| 1569 |
``` cpp
|
| 1570 |
template<class T, class U> constexpr bool operator<=(const optional<T>& x, const U& v);
|
| 1571 |
```
|
| 1572 |
|
| 1573 |
+
*Constraints:* `U` is not a specialization of `optional`. The expression
|
| 1574 |
+
`*x <= v` is well-formed and its result is convertible to `bool`.
|
| 1575 |
|
| 1576 |
*Effects:* Equivalent to: `return x.has_value() ? *x <= v : true;`
|
| 1577 |
|
| 1578 |
``` cpp
|
| 1579 |
template<class T, class U> constexpr bool operator<=(const T& v, const optional<U>& x);
|
| 1580 |
```
|
| 1581 |
|
| 1582 |
+
*Constraints:* `T` is not a specialization of `optional`. The expression
|
| 1583 |
+
`v <= *x` is well-formed and its result is convertible to `bool`.
|
| 1584 |
|
| 1585 |
*Effects:* Equivalent to: `return x.has_value() ? v <= *x : false;`
|
| 1586 |
|
| 1587 |
``` cpp
|
| 1588 |
template<class T, class U> constexpr bool operator>=(const optional<T>& x, const U& v);
|
| 1589 |
```
|
| 1590 |
|
| 1591 |
+
*Constraints:* `U` is not a specialization of `optional`. The expression
|
| 1592 |
+
`*x >= v` is well-formed and its result is convertible to `bool`.
|
| 1593 |
|
| 1594 |
*Effects:* Equivalent to: `return x.has_value() ? *x >= v : false;`
|
| 1595 |
|
| 1596 |
``` cpp
|
| 1597 |
template<class T, class U> constexpr bool operator>=(const T& v, const optional<U>& x);
|
| 1598 |
```
|
| 1599 |
|
| 1600 |
+
*Constraints:* `T` is not a specialization of `optional`. The expression
|
| 1601 |
+
`v >= *x` is well-formed and its result is convertible to `bool`.
|
| 1602 |
|
| 1603 |
*Effects:* Equivalent to: `return x.has_value() ? v >= *x : true;`
|
| 1604 |
|
| 1605 |
``` cpp
|
| 1606 |
template<class T, class U>
|
|
|
|
| 1617 |
``` cpp
|
| 1618 |
template<class T>
|
| 1619 |
constexpr void swap(optional<T>& x, optional<T>& y) noexcept(noexcept(x.swap(y)));
|
| 1620 |
```
|
| 1621 |
|
| 1622 |
+
*Constraints:*
|
| 1623 |
+
|
| 1624 |
+
``` cpp
|
| 1625 |
+
is_reference_v<T> || (is_move_constructible_v<T> && is_swappable_v<T>)
|
| 1626 |
+
```
|
| 1627 |
+
|
| 1628 |
+
is `true`.
|
| 1629 |
|
| 1630 |
*Effects:* Calls `x.swap(y)`.
|
| 1631 |
|
| 1632 |
``` cpp
|
| 1633 |
template<class T> constexpr optional<decay_t<T>> make_optional(T&& v);
|
| 1634 |
```
|
| 1635 |
|
| 1636 |
+
*Constraints:* The call to `make_optional` does not use an explicit
|
| 1637 |
+
*template-argument-list* that begins with a type *template-argument*.
|
| 1638 |
+
|
| 1639 |
*Returns:* `optional<decay_t<T>>(std::forward<T>(v))`.
|
| 1640 |
|
| 1641 |
``` cpp
|
| 1642 |
template<class T, class...Args>
|
| 1643 |
constexpr optional<T> make_optional(Args&&... args);
|