Namespaces
Variants

std::experimental:: make_unique_resource_checked

From cppreference.net
ヘッダーで定義 <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 ( /*see below*/ ) ;
(library fundamentals TS v3)

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

プログラムは、式 r == invalid 文脈的に bool へ変換できない場合 は不適格であり、変換結果が未定義動作を引き起こすか例外をスローする場合の動作は未定義です。

目次

注記: - HTMLタグと属性は一切翻訳せず、元のフォーマットを保持しています - タイポ("Paramaters"→"Parameters"、"Reture"→"Return")は原文のまま保持しています - C++固有の用語は翻訳していません - 技術文書として適切な専門的な日本語訳を使用しています

パラメータ

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

戻り値

A unique_resource は上記で説明されたものです。

例外

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

注記

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

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

#include <cstdio>
#include <experimental/scope>
int main()
{
    // fopenが失敗した場合にfcloseが呼び出されないようにする
    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.

関連項目