std::unexpected
| ヘッダ <expected> で定義 |
||
| template< class E > class unexpected; |
(C++23から) | |
クラステンプレートstd::unexpectedは、std::expectedに格納される予期しない値を表します。特に、std::expectedには、std::unexpectedを単一の引数とするコンストラクタがあり、これにより予期しない値を含むexpectedオブジェクトが作成されます。
オブジェクト型でない型、配列型、std::unexpectedの特殊化、またはcv修飾された型でunexpectedをインスタンス化すると、プログラムはill-formed(不正な形式)となります。
目次 |
[編集] テンプレートパラメータ
| E | - | 予期しない値の型。この型は、配列型、オブジェクト型でない型、std::unexpectedの特殊化、またはcv修飾された型であってはなりません。 |
[編集] メンバ関数
unexpectedオブジェクトを構築します。(public member function) | |
| (デストラクタ) (暗黙的に宣言) |
格納された値と共にunexpectedオブジェクトを破棄します。(public member function) |
| operator= (暗黙的に宣言) |
格納された値を代入します。 (public member function) |
| 格納された値にアクセスします。 (public member function) | |
| 格納された値を交換します。 (public member function) |
[編集] 非メンバ関数
| (C++23) |
格納された値を比較します。 (関数テンプレート) |
| (C++23) |
std::swap アルゴリズムを特殊化する (関数テンプレート) |
std::unexpected::unexpected
| constexpr unexpected( const unexpected& ) = default; |
(1) | |
| constexpr unexpected( unexpected&& ) = default; |
(2) | |
| template< class Err = E > constexpr explicit unexpected( Err&& e ); |
(3) | |
| template< class... Args > constexpr explicit unexpected( std::in_place_t, Args&&... args ); |
(4) | |
| template< class U, class... Args > constexpr explicit unexpected( std::in_place_t, |
(5) | |
std::unexpectedオブジェクトを構築します。
Eの値をstd::forward<Err>(e)から初期化するのと似ています。- このオーバーロードは、以下の場合にのみオーバーロード解決に参加します。
- std::is_same_v<std::remove_cvref_t<Err>, unexpected> が偽であり、
- std::is_same_v<std::remove_cvref_t<Err>, std::in_place_t> が偽であり、
- std::is_constructible_v<E, Err> が真である。
Eの値を引数std::forward<Args>(args)...から初期化するのと似ています。- このオーバーロードは、
std::is_constructible_v<E, Args...>が真である場合にのみオーバーロード解決に参加します。
Eの値を引数il, std::forward<Args>(args)...から初期化するのと似ています。- このオーバーロードは、
std::is_constructible_v<E, std::initializer_list<U>&, Args...>が真である場合にのみオーバーロード解決に参加します。
パラメータ
| e | - | 含まれる値を初期化するための値 |
| args... | - | 含まれる値を初期化するための引数 |
| il | - | 含まれる値を初期化するための初期化子リスト |
例外
Eのコンストラクタが例外をスローした場合、その例外をスローします。
std::unexpected::error
| constexpr const E& error() const& noexcept; constexpr E& error() & noexcept; |
||
格納された値への参照を返します。
std::unexpected::swap
| constexpr void swap( unexpected& other ) noexcept(std::is_nothrow_swappable_v<E>); |
||
格納された値を交換します。これは、using std::swap; swap(error(), other.error());のように呼び出すのと似ています。
std::is_swappable_v<E>が偽の場合、プログラムはill-formed(不正な形式)となります。
operator==(std::unexpected)
| template< class E2 > friend constexpr bool operator==( unexpected& x, std::unexpected<E2>& y ); |
||
格納された値を比較します。これは、return x.error() == y.error();のように呼び出すのと似ています。
式x.error() == y.error()がwell-formedでない場合、またはその結果がboolに変換できない場合、プログラムはill-formed(不正な形式)となります。
この関数は、通常の非修飾または修飾ルックアップからは見えず、std::unexpected<E>が引数の関連クラスである場合にのみ引数依存ルックアップで見つけることができます。
swap(std::unexpected)
| friend constexpr void swap( unexpected& x, unexpected& y ) noexcept(noexcept(x.swap(y))); |
||
x.swap(y)と同等です。
このオーバーロードは、std::is_swappable_v<E>が真である場合にのみオーバーロード解決に参加します。
この関数は、通常の非修飾または修飾ルックアップからは見えず、std::unexpected<E>が引数の関連クラスである場合にのみ引数依存ルックアップで見つけることができます。
[編集] 推論ガイド
| template< class E > unexpected(E) -> unexpected<E>; |
(C++23から) | |
unexpectedのための推論ガイドが提供されており、コンストラクタ引数からの推論を可能にします。
[編集] 備考
C++17より前は、名前std::unexpectedは、動的例外指定が違反されたときにC++ランタイムによって呼び出される関数を指していました。
[編集] 例
#include <expected> #include <iostream> enum class error { compile_time_error, runtime_error }; [[nodiscard]] auto unexpected_runtime_error() -> std::expected<int, error> { return std::unexpected(error::runtime_error); } int main() { std::expected<double, int> ex = std::unexpected(3); if (!ex) std::cout << "ex contains an error value\n"; if (ex == std::unexpected(3)) std::cout << "The error value is equal to 3\n"; const auto e = unexpected_runtime_error(); e.and_then([](const auto& e) -> std::expected<int, error> { std::cout << "and_then: " << int(e); // not printed return {}; }) .or_else([](const auto& e) -> std::expected<int, error> { std::cout << "or_else: " << int(e); // prints this line return {}; }); }
出力
ex contains an error value The error value is equal to 3 or_else: 1
[編集] 関連項目
expected オブジェクトを構築する(public member function) | |
| (C++23) |
expectedオブジェクトを比較します。(関数テンプレート) |