Files
cppdraft_translate/cppdraft/optional/assign.md
2025-10-25 03:02:53 +03:00

440 lines
16 KiB
Markdown
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[optional.assign]
# 22 General utilities library [[utilities]](./#utilities)
## 22.5 Optional objects [[optional]](optional#assign)
### 22.5.3 Class template optional [[optional.optional]](optional.optional#optional.assign)
#### 22.5.3.4 Assignment [optional.assign]
[🔗](#lib:operator=,optional)
`constexpr optional<T>& operator=(nullopt_t) noexcept;
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3669)
*Effects*: If *this contains a value, calls val->T::~T() to destroy the contained value; otherwise no effect[.](#1.sentence-1)
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3673)
*Postconditions*: *this does not contain a value[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3677)
*Returns*: *this[.](#3.sentence-1)
[🔗](#lib:operator=,optional_)
`constexpr optional<T>& operator=(const optional& rhs);
`
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3688)
*Effects*: See Table [67](#tab:optional.assign.copy "Table 67: optional::operator=(const optional&amp;) effects")[.](#4.sentence-1)
Table [67](#tab:optional.assign.copy) — optional::operator=(const optional&) effects [[tab:optional.assign.copy]](./tab:optional.assign.copy)
| [🔗](#tab:optional.assign.copy-row-1) | | ***this contains a value** | ***this does not contain a value** |
| --- | --- | --- | --- |
| [🔗](#tab:optional.assign.copy-row-2)<br>**rhs contains a value** | | assigns *rhs to the contained value | direct-non-list-initializes the contained value with *rhs |
| [🔗](#tab:optional.assign.copy-row-3)<br>**rhs does not contain a value** | | destroys the contained value by calling val->T::~T() | no effect |
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3705)
*Postconditions*: rhs.has_value() == this->has_value()[.](#5.sentence-1)
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3709)
*Returns*: *this[.](#6.sentence-1)
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3713)
*Remarks*: If any exception is thrown, the result of the expression this->has_value() remains unchanged[.](#7.sentence-1)
If an exception is thrown during the call to T's copy constructor, no effect[.](#7.sentence-2)
If an exception is thrown during the call to T's copy assignment,
the state of its contained value is as defined by the exception safety guarantee of T's copy assignment[.](#7.sentence-3)
This operator is defined as deleted unlessis_copy_constructible_v<T> is true andis_copy_assignable_v<T> is true[.](#7.sentence-4)
If is_trivially_copy_constructible_v<T> &&is_trivially_copy_assignable_v<T> &&is_trivially_destructible_v<T> is true,
this assignment operator is trivial[.](#7.sentence-5)
[🔗](#lib:operator=,optional__)
`constexpr optional& operator=(optional&& rhs) noexcept(see below);
`
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3734)
*Constraints*: is_move_constructible_v<T> is true andis_move_assignable_v<T> is true[.](#8.sentence-1)
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3739)
*Effects*: See Table [68](#tab:optional.assign.move "Table 68: optional::operator=(optional&amp;&amp;) effects")[.](#9.sentence-1)
The result of the expression rhs.has_value() remains unchanged[.](#9.sentence-2)
Table [68](#tab:optional.assign.move) — optional::operator=(optional&&) effects [[tab:optional.assign.move]](./tab:optional.assign.move)
| [🔗](#tab:optional.assign.move-row-1) | | ***this contains a value** | ***this does not contain a value** |
| --- | --- | --- | --- |
| [🔗](#tab:optional.assign.move-row-2)<br>**rhs contains a value** | | assigns *std::move(rhs) to the contained value | direct-non-list-initializes the contained value with *std::move(rhs) |
| [🔗](#tab:optional.assign.move-row-3)<br>**rhs does not contain a value** | | destroys the contained value by calling val->T::~T() | no effect |
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3757)
*Postconditions*: rhs.has_value() == this->has_value()[.](#10.sentence-1)
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3761)
*Returns*: *this[.](#11.sentence-1)
[12](#12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3765)
*Remarks*: The exception specification is equivalent to:is_nothrow_move_assignable_v<T> && is_nothrow_move_constructible_v<T>
[13](#13)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3772)
If any exception is thrown, the result of the expression this->has_value() remains unchanged[.](#13.sentence-1)
If an exception is thrown during the call to T's move constructor,
the state of *rhs.val is determined by the exception safety guarantee of T's move constructor[.](#13.sentence-2)
If an exception is thrown during the call to T's move assignment,
the state of *val and *rhs.val is determined by the exception safety guarantee of T's move assignment[.](#13.sentence-3)
If is_trivially_move_constructible_v<T> &&is_trivially_move_assignable_v<T> &&is_trivially_destructible_v<T> is true,
this assignment operator is trivial[.](#13.sentence-4)
[🔗](#lib:operator=,optional___)
`template<class U = remove_cv_t<T>> constexpr optional& operator=(U&& v);
`
[14](#14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3790)
*Constraints*:
- [(14.1)](#14.1)
is_same_v<remove_cvref_t<U>, optional> is false,
- [(14.2)](#14.2)
conjunction_v<is_scalar<T>, is_same<T, decay_t<U>>> is false,
- [(14.3)](#14.3)
is_constructible_v<T, U> is true, and
- [(14.4)](#14.4)
is_assignable_v<T&, U> is true[.](#14.sentence-1)
[15](#15)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3799)
*Effects*: If *this contains a value, assigns std::forward<U>(v) to the contained value; otherwise direct-non-list-initializes the contained value with std::forward<U>(v)[.](#15.sentence-1)
[16](#16)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3803)
*Postconditions*: *this contains a value[.](#16.sentence-1)
[17](#17)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3807)
*Returns*: *this[.](#17.sentence-1)
[18](#18)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3811)
*Remarks*: If any exception is thrown, the result of the expression this->has_value() remains unchanged[.](#18.sentence-1)
If an exception is thrown during the call to T's constructor, the state of v is determined by the exception safety guarantee of T's constructor[.](#18.sentence-2)
If an exception is thrown during the call to T's assignment, the state of *val and v is determined by the exception safety guarantee of T's assignment[.](#18.sentence-3)
[🔗](#lib:operator=,optional____)
`template<class U> constexpr optional<T>& operator=(const optional<U>& rhs);
`
[19](#19)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3822)
*Constraints*:
- [(19.1)](#19.1)
is_constructible_v<T, const U&> is true,
- [(19.2)](#19.2)
is_assignable_v<T&, const U&> is true,
- [(19.3)](#19.3)
*converts-from-any-cvref*<T, optional<U>> is false,
- [(19.4)](#19.4)
is_assignable_v<T&, optional<U>&> is false,
- [(19.5)](#19.5)
is_assignable_v<T&, optional<U>&&> is false,
- [(19.6)](#19.6)
is_assignable_v<T&, const optional<U>&> is false, and
- [(19.7)](#19.7)
is_assignable_v<T&, const optional<U>&&> is false[.](#19.sentence-1)
[20](#20)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3834)
*Effects*: See Table [69](#tab:optional.assign.copy.templ "Table 69: optional::operator=(const optional<U>&amp;) effects")[.](#20.sentence-1)
Table [69](#tab:optional.assign.copy.templ) — optional::operator=(const optional<U>&) effects [[tab:optional.assign.copy.templ]](./tab:optional.assign.copy.templ)
| [🔗](#tab:optional.assign.copy.templ-row-1) | | ***this contains a value** | ***this does not contain a value** |
| --- | --- | --- | --- |
| [🔗](#tab:optional.assign.copy.templ-row-2)<br>**rhs contains a value** | | assigns *rhs to the contained value | direct-non-list-initializes the contained value with *rhs |
| [🔗](#tab:optional.assign.copy.templ-row-3)<br>**rhs does not contain a value** | | destroys the contained value by calling val->T::~T() | no effect |
[21](#21)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3851)
*Postconditions*: rhs.has_value() == this->has_value()[.](#21.sentence-1)
[22](#22)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3855)
*Returns*: *this[.](#22.sentence-1)
[23](#23)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3859)
*Remarks*: If any exception is thrown,
the result of the expression this->has_value() remains unchanged[.](#23.sentence-1)
If an exception is thrown during the call to T's constructor,
the state of *rhs.val is determined by
the exception safety guarantee of T's constructor[.](#23.sentence-2)
If an exception is thrown during the call to T's assignment,
the state of *val and *rhs.val is determined by
the exception safety guarantee of T's assignment[.](#23.sentence-3)
[🔗](#lib:operator=,optional_____)
`template<class U> constexpr optional<T>& operator=(optional<U>&& rhs);
`
[24](#24)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3877)
*Constraints*:
- [(24.1)](#24.1)
is_constructible_v<T, U> is true,
- [(24.2)](#24.2)
is_assignable_v<T&, U> is true,
- [(24.3)](#24.3)
*converts-from-any-cvref*<T, optional<U>> is false,
- [(24.4)](#24.4)
is_assignable_v<T&, optional<U>&> is false,
- [(24.5)](#24.5)
is_assignable_v<T&, optional<U>&&> is false,
- [(24.6)](#24.6)
is_assignable_v<T&, const optional<U>&> is false, and
- [(24.7)](#24.7)
is_assignable_v<T&, const optional<U>&&> is false[.](#24.sentence-1)
[25](#25)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3889)
*Effects*: See Table [70](#tab:optional.assign.move.templ "Table 70: optional::operator=(optional<U>&amp;&amp;) effects")[.](#25.sentence-1)
The result of the expression rhs.has_value() remains unchanged[.](#25.sentence-2)
Table [70](#tab:optional.assign.move.templ) — optional::operator=(optional<U>&&) effects [[tab:optional.assign.move.templ]](./tab:optional.assign.move.templ)
| [🔗](#tab:optional.assign.move.templ-row-1) | | ***this contains a value** | ***this does not contain a value** |
| --- | --- | --- | --- |
| [🔗](#tab:optional.assign.move.templ-row-2)<br>**rhs contains a value** | | assigns *std::move(rhs) to the contained value | direct-non-list-initializes the contained value with *std::move(rhs) |
| [🔗](#tab:optional.assign.move.templ-row-3)<br>**rhs does not contain a value** | | destroys the contained value by calling val->T::~T() | no effect |
[26](#26)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3907)
*Postconditions*: rhs.has_value() == this->has_value()[.](#26.sentence-1)
[27](#27)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3911)
*Returns*: *this[.](#27.sentence-1)
[28](#28)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3915)
*Remarks*: If any exception is thrown,
the result of the expression this->has_value() remains unchanged[.](#28.sentence-1)
If an exception is thrown during the call to T's constructor,
the state of *rhs.val is determined by
the exception safety guarantee of T's constructor[.](#28.sentence-2)
If an exception is thrown during the call to T's assignment,
the state of *val and *rhs.val is determined by
the exception safety guarantee of T's assignment[.](#28.sentence-3)
[🔗](#lib:emplace,optional)
`template<class... Args> constexpr T& emplace(Args&&... args);
`
[29](#29)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3933)
*Mandates*: is_constructible_v<T, Args...> is true[.](#29.sentence-1)
[30](#30)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3937)
*Effects*: Calls *this = nullopt[.](#30.sentence-1)
Then direct-non-list-initializes the contained value
with std::forward<Args>(args)...[.](#30.sentence-2)
[31](#31)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3942)
*Postconditions*: *this contains a value[.](#31.sentence-1)
[32](#32)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3946)
*Returns*: A reference to the new contained value[.](#32.sentence-1)
[33](#33)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3950)
*Throws*: Any exception thrown by the selected constructor of T[.](#33.sentence-1)
[34](#34)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3954)
*Remarks*: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous *val (if any) has been destroyed[.](#34.sentence-1)
[🔗](#lib:emplace,optional_)
`template<class U, class... Args> constexpr T& emplace(initializer_list<U> il, Args&&... args);
`
[35](#35)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3965)
*Constraints*: is_constructible_v<T, initializer_list<U>&, Args...> is true[.](#35.sentence-1)
[36](#36)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3969)
*Effects*: Calls *this = nullopt[.](#36.sentence-1)
Then direct-non-list-initializes the contained value withil, std::forward<Args>(args)...[.](#36.sentence-2)
[37](#37)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3974)
*Postconditions*: *this contains a value[.](#37.sentence-1)
[38](#38)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3978)
*Returns*: A reference to the new contained value[.](#38.sentence-1)
[39](#39)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3982)
*Throws*: Any exception thrown by the selected constructor of T[.](#39.sentence-1)
[40](#40)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/utilities.tex#L3986)
*Remarks*: If an exception is thrown during the call to T's constructor, *this does not contain a value, and the previous *val (if any) has been destroyed[.](#40.sentence-1)