std::expected<T,E>::operator=
constexpr expected& operator=( const expected& other );
|
(1) | (自 C++23 起) |
constexpr expected& operator=( expected&& other ) noexcept(/*see below*/);
|
(2) | (自 C++23 起) |
template< class U = std::remove_cv_t<T> >
constexpr expected& operator=( U&& v );
|
(3) | (自 C++23 起) (T 不是 cv void) |
template< class G >
constexpr expected& operator=( const unexpected<G>& other );
|
(4) | (自 C++23 起) |
template< class G >
constexpr expected& operator=( unexpected<G>&& other );
|
(5) | (自 C++23 起) |
向已有的expected对象赋予新值。
other的状态。
- 若
this->has_value()等于other.has_value(),则赋值other所含的值。如果T是(可能有 cv 限定)void且other.has_value()是true,则不执行任何操作。 - 否则,销毁当前所含的值(若
this->has_value()是true且T是(可能有 cv 限定)void,则不执行任何操作),并使*this包含other所含值的副本。
- 如果
other.has_value()是true且T是(可能有 cv 限定)void,则不构造新值。否则,新值通过复制构造(1)或移动构造(2)自*other或other.error()(视情况而定)。如果抛出异常,则保留旧值;*this不会变为无值状态。
如果没有抛出异常,则赋值后,has_value()等于other.has_value()。
- 重载(1)被定义为弃置,除非:
- 要么
T是(可能有 cv 限定)void或std::is_copy_assignable_v<T>是true,且 - 要么
T是(可能有 cv 限定)void或std::is_copy_constructible_v<T>是true,且 std::is_copy_assignable_v<E>是true,且std::is_copy_constructible_v<E>是true,且- 以下至少一项为
true:T是(可能有 cv 限定)voidstd::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
- 要么
- 重载(2)仅在以下条件下参与重载决议:
- 要么
T是(可能有 cv 限定)void或std::is_move_assignable_v<T>是true,且 - 要么
T是(可能有 cv 限定)void或std::is_move_constructible_v<T>是true,且 std::is_move_assignable_v<E>是true,且std::is_move_constructible_v<E>是true,且- 以下至少一项为
true:T是(可能有 cv 限定)voidstd::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
- 要么
- 如果
this->has_value()是true,等价于**this = std::forward<U>(v)。 - 否则,销毁
*this中所含的值,并使*this包含从std::forward<U>(v)初始化的值。如果抛出异常,则保留旧值;*this不会变为无值状态。
如果没有抛出异常,则赋值后,this->has_value()为true。
- 此重载仅在以下条件下参与重载决议:
std::is_same_v<expected, std::remove_cvref_t<U>>是false,且std::remove_cvref_t<U>不是std::unexpected的特化,且std::is_constructible_v<T, U>是true,且std::is_assignable_v<T&, U>是true,且- 以下至少一项为
true:std::is_nothrow_constructible_v<T, U>std::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
- 如果
this->has_value()是true,则销毁*this中所含的值(若T是(可能有 cv 限定)void,则不执行任何操作),并使*this包含从std::forward<GF>(e.error())初始化的值。如果抛出异常,则保留旧值;*this不会变为无值状态。 - 否则,等价于
this->error() = std::forward<GF>(e.error())。
如果没有抛出异常,则赋值后,this->has_value()为false。
- 此重载仅在以下条件下参与重载决议:
std::is_constructible_v<E, GF>是true,且std::is_assignable_v<E&, GF>是true,且- 以下至少一项为
true:T是(可能有 cv 限定)voidstd::is_nothrow_constructible_v<E, GF>std::is_nothrow_move_constructible_v<T>std::is_nothrow_move_constructible_v<E>
在所有情况下,如果T不是(可能有 cv 限定)void,则旧值的销毁和新值的构造通过以下仅用于阐述的函数reinit_expected执行。
template<class NewType, class OldType, class... Args>
constexpr void reinit_expected(NewType& new_val, OldType& old_val, Args&&... args)
{
if constexpr (std::is_nothrow_constructible_v<NewType, Args...>)
{
std::destroy_at(std::addressof(old_val));
std::construct_at(std::addressof(new_val), std::forward<Args>(args)...);
}
else if constexpr (std::is_nothrow_move_constructible_v<NewType>)
{
NewType temp(std::forward<Args>(args)...); // may throw
std::destroy_at(std::addressof(old_val));
std::construct_at(std::addressof(new_val), std::move(temp));
}
else
{
OldType temp(std::move(old_val));
std::destroy_at(std::addressof(old_val));
try
{
std::construct_at(std::addressof(new_val), std::forward<Args>(args)...); // may throw
}
catch (...)
{
std::construct_at(std::addressof(old_val), std::move(temp));
throw;
}
}
}
参数
| other | - |
另一个
expected
对象,其包含的值将被赋值
|
| v | - | 要赋值给所包含值的值 |
| e | - | std::unexpected 对象,其包含的值将被赋值 |
| newval | - | 将要被构造的包含值 |
| oldval | - | 将要被销毁的包含值 |
| args | - | 用作 newval 初始化器的参数 |
返回值
异常
std::
is_nothrow_move_constructible_v
<
T
>
&&
std::
is_nothrow_move_assignable_v
<
T
>
&&
示例
|
本节内容不完整
原因:缺少示例 |
缺陷报告
下列行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 3886 | C++23 |
重载版本
(
3
)
的默认模板参数是
T
|
改为 std:: remove_cv_t < T > |
| LWG 4025 | C++23 |
当
E
不可移动构造或不可移动赋值时,
重载版本 ( 7 ) 被定义为已删除 |
在此情况下不参与
重载决议 |
参见
|
在原位构造期望值
(公开成员函数) |