std::expected<T,E>::expected

来自cppreference.com
 
 
 
 
constexpr expected();
(1) (C++23 起)
constexpr expected( const expected& other );
(2) (C++23 起)
constexpr expected( expected&& other ) noexcept(/* 见下文 */);
(3) (C++23 起)
template< class U, class G >
constexpr explicit(/* 见下文 */) expected( const expected<U, G>& other );
(4) (C++23 起)
template< class U, class G >
constexpr explicit(/* 见下文 */) expected( expected<U, G>&& other );
(5) (C++23 起)
template< class U = std::remove_cv_t<T> >
constexpr explicit(!std::is_convertible_v<U, T>) expected( U&& v );
(6) (C++23 起)
(T 不是 cv void)
template< class G >
constexpr explicit(!std::is_convertible_v<const G&, E>)
    expected( const std::unexpected<G>& e );
(7) (C++23 起)
template< class G >
constexpr explicit(!std::is_convertible_v<G, E>)
    expected( std::unexpected<G>&& e );
(8) (C++23 起)
template< class... Args >
constexpr explicit expected( std::in_place_t, Args&&... args );
(9) (C++23 起)
(T 不是 cv void)
template< class U, class... Args >
constexpr explicit expected( std::in_place_t,
                             std::initializer_list<U> il, Args&&... args );
(10) (C++23 起)
(T 不是 cv void)
constexpr explicit expected( std::in_place_t ) noexcept;
(11) (C++23 起)
(T 是 cv void)
template< class... Args >
constexpr explicit expected( std::unexpect_t, Args&&... args );
(12) (C++23 起)
template< class U, class... Args >
constexpr explicit expected( std::unexpect_t,
                             std::initializer_list<U> il, Args&&... args );
(13) (C++23 起)

构造新的 expected 对象。

1) 默认构造函数。如果 T 不是(可能有 cv 限定的)void,则构造一个包含预期值的对象,该预期值被值初始化
构造完成后,has_value() 返回 true
此重载只有在T 是(可能有 cv 限定的)void,或者 std::is_default_constructible_v<T>true 时才会参与重载决议。
2) 复制构造函数。如果 other.has_value()false,则新对象包含一个非预期值,该值从 other.error() 直接初始化。否则,如果 T 不是(可能有 cv 限定的)void,则新对象包含一个预期值,该值从 *other 直接初始化
构造完成后,has_value() 等于 other.has_value()
该构造函数被定义为弃置的,除非满足以下条件:
  • 要么 T 是(可能有 cv 限定的)void,要么 std::is_copy_constructible_v<T>true,且
  • std::is_copy_constructible_v<E>true
如果满足以下条件,则该构造函数是平凡的:
  • 要么 T 是(可能有 cv 限定的)void,要么 std::is_trivially_copy_constructible_v<T>true,且
  • std::is_trivially_copy_constructible_v<E>true
3) 移动构造函数。如果 other.has_value()false,则新对象包含一个非预期值,该值从 std::move(other.error()) 直接初始化。否则,如果 T 不是(可能有 cv 限定的)void,则新对象包含一个预期值,该值从 std::move(*other) 直接初始化
构造完成后,has_value() 等于 other.has_value()
仅当满足以下条件时,该构造函数才参与重载决议:
  • 要么 T 是(可能有 cv 限定的)void,要么 std::is_move_constructible_v<T>true,且
  • std::is_move_constructible_v<E>true
如果满足以下条件,则该构造函数是平凡的:
  • std::is_trivially_move_constructible_v<T>true,且
  • std::is_trivially_move_constructible_v<E>true
4,5)
  • UF 对于 (4)std::add_lvalue_reference_t<const U>,对于 (5)U,且
  • GF 对于 (4)const G&,对于 (5)G
如果 other.has_value()false,则新对象包含一个非预期值,该值从 std::forward<GF>(other.error()) 直接初始化。否则,如果 T 不是(可能有 cv 限定的)void,则新对象包含一个预期值,该值从 std::forward<UF>(*other) 直接初始化
构造完成后,has_value() 等于 other.has_value()
这些构造函数各自仅在分别满足以下条件时参与重载决议:
  • 要么
    • T 是(可能有 cv 限定的)void,且 std::is_void_v<U>true,要么
    • std::is_constructible_v<T, UF>true
  • std::is_constructible_v<E, GF>true
  • 如果 T 不是(可能有 cv 限定的)bool,则 T 不能从任何类型为(可能有 const 的)std::expected<U, G> 的表达式构造或转换,即以下 8 个值均为 false
    • std::is_constructible_v<T, std::expected<U, G>&
    • std::is_constructible_v<T, std::expected<U, G>
    • std::is_constructible_v<T, const std::expected<U, G>&
    • std::is_constructible_v<T, const std::expected<U, G>
    • std::is_convertible_v<std::expected<U, G>&, T>
    • std::is_convertible_v<std::expected<U, G>, T>
    • std::is_convertible_v<const std::expected<U, G>&, T>
    • std::is_convertible_v<const std::expected<U, G>, T>
  • std::unexpected<E> 不能从任何类型为(可能有 const 的)std::expected<U, G> 的表达式构造,即以下 4 个值均为 false
    • std::is_constructible_v<std::unexpected<E>, std::expected<U, G>&
    • std::is_constructible_v<std::unexpected<E>, std::expected<U, G>
    • std::is_constructible_v<std::unexpected<E>, const std::expected<U, G>&
    • std::is_constructible_v<std::unexpected<E>, const std::expected<U, G>
如果 std::is_convertible_v<UF, T>std::is_convertible_v<GF, E>false,则这些构造函数为 explicit
6) 构造一个包含预期值的对象,该预期值如同使用表达式 std::forward<U>(v)T 类型的对象进行直接初始化(而非直接列表初始化)来初始化。
构造完成后,has_value() 返回 true
除非满足以下条件,否则该构造函数不参与重载决议:
  • T 不是(可能有 cv 限定的)void
  • std::is_same_v<std::remove_cvref_t<U>, std::in_place_t>false
  • std::is_same_v<expected, std::remove_cvref_t<U>>false
  • std::is_constructible_v<T, U>true
  • std::remove_cvref_t<U> 不是 std::unexpected 的特化。
  • 如果 T 是(可能有 cv 限定的)bool,则 std::remove_cvref_t<U> 不是 std::expected 的特化。
7,8)GF 对于 (7)const G&,对于 (8)G。 构造一个包含非预期值的对象,该非预期值从 std::forward<GF>(e.error()) 直接初始化
构造完成后,has_value() 返回 false
这些重载只有在std::is_constructible_v<E, GF>true 时才会参与重载决议。
9) 构造一个包含预期值的对象,该预期值从实参 std::forward<Args>(args)... 直接初始化
构造完成后,has_value() 返回 true
此重载只有在std::is_constructible_v<T, Args...>true 时才会参与重载决议。
10) 构造一个包含预期值的对象,该预期值从实参 il, std::forward<Args>(args)... 直接初始化
构造完成后,has_value() 返回 true
此重载只有在std::is_constructible_v<T, std::initializer_list<U>&, Args...>true 时才会参与重载决议。
11) 构造一个对象,使得构造完成后 has_value() 返回 true
12) 构造一个包含非预期值的对象,该非预期值从实参 std::forward<Args>(args)... 直接初始化
构造完成后,has_value() 返回 false
此重载只有在std::is_constructible_v<E, Args...>true 时才会参与重载决议。
13) 构造一个包含非预期值的对象,该非预期值从实参 il, std::forward<Args>(args)... 直接初始化
构造完成后,has_value() 返回 false
此重载只有在std::is_constructible_v<E, std::initializer_list<U>&, Args...>true 时才会参与重载决议。

参数

other - 另一个 expected 对象,它包含的值会被复制
e - std::unexpected 对象,它包含的值会被复制
v - 用来初始化包含值的值
args... - 用来初始化包含值的实参
il - 用来初始化包含值的初始化器列表

异常

1) 抛出由 T 的构造函数抛出的任何异常。
如果 T 是(可能有 cv 限定的)void,则
noexcept 说明:  
noexcept
  
2) 抛出由 TE 的构造函数抛出的任何异常。
3) 如果 T 是(可能有 cv 限定的)void, 则
noexcept 说明:  
noexcept(std::is_nothrow_move_constructible_v<E>)
否则,
noexcept 说明:  
noexcept(std::is_nothrow_move_constructible_v<T>
     && std::is_nothrow_move_constructible_v<E>)
4,5) 抛出由 TE 的构造函数抛出的任何异常。
6) 抛出由 T 的构造函数抛出的任何异常。
7,8) 抛出由 E 的构造函数抛出的任何异常。
9,10) 抛出由 T 的构造函数抛出的任何异常。
12,13) 抛出由 E 的构造函数抛出的任何异常。

示例

缺陷报告

下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。

缺陷报告 应用于 出版时的行为 正确行为
LWG 3886 C++23 重载 (6) 的默认模板实参是 T 改成 std::remove_cv_t<T>

参阅

表示一个非预期值
(类模板) [编辑]
原位构造标签
(类模板) [编辑]
expected 中非预期值的原位构造标签
(类) (常量) [编辑]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.