Namespaces
Variants

std:: unexpected

From cppreference.net
Utilities library
ヘッダーで定義 <expected>
template < class E >
class unexpected ;
(C++23以降)

クラステンプレート std::unexpected は、 std::expected に格納される予期しない値を表します。特に、 std::expected には std::unexpected を単一引数とするコンストラクタがあり、これにより予期しない値を含む expected オブジェクトが生成されます。

プログラムは、非オブジェクト型、配列型、 unexpected の特殊化、またはCV修飾型で std::unexpected をインスタンス化した場合、ill-formedとなります。

目次

テンプレートパラメータ

E - 予期せぬ値の型。この型は配列型、非オブジェクト型、 std::unexpected の特殊化、またはCV修飾型であってはなりません。

メンバー関数

unexpected オブジェクトを構築する
(public member function)
(destructor)
(implicitly declared)
unexpected オブジェクトと格納された値を破棄する
(public member function)
operator=
(implicitly declared)
格納された値を代入する
(public member function)
格納された値にアクセスする
(public member function)
格納された値を交換する
(public member function)

非メンバー関数

(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 ,

std:: initializer_list < U > il, Args && ... args ) ;
(5)

std::unexpected オブジェクトを構築します。

1,2) コピー/ムーブコンストラクタ。それぞれ格納された値をコピーまたはムーブします。
3) 格納されている値を、型 E の値を 直接初期化 によって std:: forward < Err > ( e ) から構築する。
4) 格納された値を構築します。これは、引数 std:: forward < Args > ( args ) ... から型 E の値を 直接初期化 するかのように行われます。
  • このオーバーロードは、 std:: is_constructible_v < E, Args... > がtrueの場合にのみオーバーロード解決に参加します。
5) 格納されている値を、型 E の値を引数 il, std:: forward < Args > ( args ) ... から 直接初期化 によって構築する。

パラメータ

e - 格納される値を初期化するための値
args... - 格納される値を初期化するための引数
il - 格納される値を初期化するための初期化子リスト

例外

E のコンストラクタによってスローされるあらゆる例外をスローします。

std::unexpected:: error

constexpr const E & error ( ) const & noexcept ;

constexpr E & error ( ) & noexcept ;
constexpr const E && error ( ) const && noexcept ;

constexpr E && error ( ) && noexcept ;

格納された値への参照を返します。

(注:指定されたテキストブロック内に翻訳対象となる可読テキストが存在しないため、HTML構造はそのまま保持されています)

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 > がfalseの場合、プログラムは不適格です。

operator== (std::unexpected)

template < class E2 >
friend constexpr bool operator == ( unexpected & x, std :: unexpected < E2 > & y ) ;

格納されている値を比較します。比較は return x. error ( ) == y. error ( ) によって行われます。

x. error ( ) == e. error ( ) が適切な形式ではない場合、またはその結果が bool に変換できない場合、プログラムは不適格となります。

この関数は通常の 非修飾名探索 または 修飾名探索 では可視ではなく、 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> が true の場合にのみオーバーロード解決に参加します。

この関数は通常の 非修飾名探索 または 修飾名探索 では可視ではなく、 std::unexpected<E> が引数の関連クラスである場合にのみ 実引数依存探索 によって発見されます。

推論ガイド

template < class E >
unexpected ( E ) - > unexpected < E > ;
(C++23以降)

deduction guide 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)
期待される値を返す
(public member function)
内容を交換する
(public member function)
std::swap アルゴリズムを特殊化する
(function)
(C++23)
expected オブジェクトを比較する
(function template)