std::optional<T>::operator=
來自 cppreference.com
optional& operator=( std::nullopt_t ) noexcept; |
(1) | (C++17 起) (C++20 起為 constexpr) |
constexpr optional& operator=( const optional& other ); |
(2) | (C++17 起) |
constexpr optional& operator= ( optional&& other ) noexcept(/* see below */); |
(3) | (C++17 起) |
template< class U > optional& operator=( const optional<U>& other ); |
(4) | (C++17 起) (C++20 起為 constexpr) |
template< class U > optional& operator=( optional<U>&& other ); |
(5) | (C++17 起) (C++20 起為 constexpr) |
template< class U = std::remove_cv_t<T> > optional& operator=( U&& value ); |
(6) | (C++17 起) (C++20 起為 constexpr) |
將 other 的內容替換到 *this 中。
2-5) 賦值 other 的狀態。has_value() 在此呼叫後返回 other.has_value()。
效果 | *this 包含值 | *this 不包含值 |
---|---|---|
other 包含值 |
|
|
other 不包含值 | 透過呼叫 val ->T::~T() 銷燬包含的值 |
無效果 |
如果 std::is_trivially_copy_constructible_v<T>、std::is_trivially_copy_assignable_v<T> 和 std::is_trivially_destructible_v<T> 都為 true,則賦值運算子是平凡的。
如果 std::is_trivially_move_constructible_v<T>、std::is_trivially_move_assignable_v<T> 和 std::is_trivially_destructible_v<T> 都為 true,則賦值運算子是平凡的。
4,5) 這些過載僅在以下所有條件都滿足時參與過載決議:
- 以下 12 個值都為 false[1]
- std::is_constructible_v<T, std::optional<U>&>
- std::is_constructible_v<T, const std::optional<U>&>
- std::is_constructible_v<T, std::optional<U>&&>
- std::is_constructible_v<T, const std::optional<U>&&>
- std::is_convertible_v<std::optional<U>&, T>
- std::is_convertible_v<const std::optional<U>&, T>
- std::is_convertible_v<std::optional<U>&&, T>
- std::is_convertible_v<const std::optional<U>&&, T>
- std::is_assignable_v<T&, std::optional<U>&>
- std::is_assignable_v<T&, const std::optional<U>&>
- std::is_assignable_v<T&, std::optional<U>&&>
- std::is_assignable_v<T&, const std::optional<U>&&>
- 對於過載 (4),std::is_constructible_v<T, const U&> 和 std::is_assignable_v<T&, const U&> 都為 true。
- 對於過載 (5),std::is_constructible_v<T, U> 和 std::is_assignable_v<T&, U> 都為 true。
6) 如果 *this 包含值,則將 std::forward<U>(value) 賦值給包含的值;否則用 std::forward<U>(value) 直接非列表初始化包含的值。*this 在此呼叫後包含值。
僅當滿足以下所有條件時,此過載才參與過載決議
- std::decay_t<U>(直到 C++20)std::remove_cvref_t<U>(自 C++20 起) 不是 std::optional<T>。
- std::is_constructible_v<T, U> 為 true。
- std::is_assignable_v<T&, U> 為 true。
- 滿足以下任何條件:
-
T
不是標量型別。 - std::decay_t<U> 不是
T
。
-
- ↑ 換句話說,
T
不能從任何型別(可能為 const 限定)的表示式 std::optional<U> 構造、轉換或賦值
目錄 |
[編輯] 引數
其他 | - | other |
value | - | 要賦值給包含值的值 |
[編輯] 返回值
*this
[編輯] 異常
2-6) 丟擲由
T
的建構函式或賦值運算子丟擲的任何異常。如果丟擲異常,*this(以及在 (2-5) 情況下的 other)的初始化狀態保持不變,即如果物件包含值,則它仍然包含值,反之亦然。value 的內容以及 *this 和 other 的包含值取決於異常來源操作(複製建構函式、移動賦值等)的異常安全保證。3) 具有以下特性:
noexcept 規範:
noexcept(std::is_nothrow_move_assignable_v<T> &&
std::is_nothrow_move_constructible_v<T>)
std::is_nothrow_move_constructible_v<T>)
[編輯] 注意
可選物件 op 可以透過 op = {}; 和 op = nullopt; 變成空可選。第一個表示式用 {} 構造一個空 optional
物件並將其賦值給 op。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_optional |
202106L |
(C++20) (DR20) |
完全 constexpr (1), (4-6) |
[編輯] 示例
執行此程式碼
#include <iostream> #include <optional> int main() { std::optional<const char*> s1 = "abc", s2; // constructor s2 = s1; // assignment s1 = "def"; // decaying assignment (U = char[4], T = const char*) std::cout << *s2 << ' ' << *s1 << '\n'; }
輸出
abc def
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 3886 | C++17 | 過載 (6) 的預設模板引數是 T |
改為 std::remove_cv_t<T> |
P0602R4 | C++17 | 複製/移動賦值運算子可能不平凡 即使底層操作是平凡的 |
要求傳播平凡性 |
P2231R1 | C++20 | 過載 (1,4-6) 不是 constexpr | 設為 constexpr |
[編輯] 參閱
就地構造包含的值 (public member function) |