名前空間
変種
操作

std::experimental::make_unique_resource_checked

From cppreference.com
 
 
 
 
 
ヘッダ <experimental/scope> で定義
template< class R, class D, class S = std::decay_t<R> >

std::experimental::unique_resource<std::decay_t<R>, std::decay_t<D>>
    make_unique_resource_checked( R&& r, const S& invalid, D&& d )

    noexcept(/*下記参照*/);
(ライブラリ基本TS v3)

unique_resource を作成し、格納されているリソースハンドルを std::forward<R>(r) で、デリータを std::forward<D>(d) で初期化します。作成された unique_resource は、 bool(r == invalid)false である場合にのみ、リソースを所有します。

r == invalid文脈変換により bool に変換できない場合、プログラムは不定形となり、変換が未定義の動作を引き起こすか例外をスローする場合、その動作は未定義です。

目次

[編集] パラメータ

r - リソースハンドル
d - リソースを破棄するために使用するデリータ
無効値 - リソースハンドルが無効であることを示す値

[編集] 戻り値

上記で説明した unique_resource

[編集] 例外

格納されているリソースハンドルとデリータの初期化中にスローされた例外。

[編集] 注意

make_unique_resource_checked は、無効な引数でデリータ関数を呼び出すことを避けるために存在します。

リソースハンドル r は、戻り値にコピーまたは移動され、作成された unique_resource は常にオブジェクト型を持つ基となるリソースハンドルを保持します。

[編集]

#include <cstdio>
#include <experimental/scope>
 
int main()
{
    // avoid calling fclose when fopen fails
    auto file = std::experimental::make_unique_resource_checked(
        std::fopen("potentially_nonexistent_file.txt", "r"),
        nullptr,
        [](std::FILE *fptr) { std::fclose(fptr); }
    );
    if (file.get())
        std::puts("The file exists.");
    else
        std::puts("The file does not exist.");
}

実行結果の例

The file does not exist.

[編集] 関連項目

English 日本語 中文(简体) 中文(繁體)