std::experimental::make_unique_resource_checked
From cppreference.com
< cpp | experimental | unique resource
| ヘッダ <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>> |
(ライブラリ基本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。
[編集] 例外
格納されているリソースハンドルとデリータの初期化中にスローされた例外。
noexcept 指定:
noexcept(
std::is_nothrow_constructible_v<std::decay_t<R>, R> &&
std::is_nothrow_constructible_v<std::decay_t<D>, D>
[編集] 注意
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.