QScopeGuard Class
template <typename F> class QScopeGuardスコープの最後で関数を呼び出すためのスコープガードを提供します。詳細...
ヘッダ | #include <QScopeGuard> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
パブリック関数
QScopeGuard(F &&f) | |
QScopeGuard(const F &f) | |
void | dismiss() |
関連する非メンバー
QScopeGuard<typename std::decay<F>::type> | qScopeGuard(F &&f) |
詳しい説明
QScopeGuard<F>は、デストラクタで関数f を実行することだけを目的としたクラスです。これは、関数が正常に終了した場合でも、return文によって早期に終了した場合でも、例外によって終了した場合でも、クリーンアップ・コードが実行されることを保証するのに便利です。
注意: 例外はサポートされていません。callable は、実行時、コピー時、移動時に throw してはいけません。
QScopedValueRollbackも参照してください 。
メンバ関数ドキュメント
[explicit noexcept]
QScopeGuard::QScopeGuard(F &&f)
[explicit noexcept]
QScopeGuard::QScopeGuard(const F &f)
スコープの最後にf を実行するスコープ・ガードを作る。
Fがラムダの場合、その型を書くことはできません。その場合は、クラス・テンプレートの引数控除(C++17の機能)に頼ってテンプレート・パラメーターを完全に除外するか、このコンストラクターの代わりにヘルパー関数qScopeGuard ()を使用する必要があります。
[noexcept]
void QScopeGuard::dismiss()
スコープガードを解除し、関数Fがスコープの最後で呼び出されないようにする。
関連する非会員
template <typename F> QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)
qScopeGuard 関数を使うと、スコープの最後で関数を呼び出すことができます。
スコープの最後にf を実行するスコープガードを作成します。
このヘルパー関数は、呼び出し可能な型のテンプレート・パラメータに名前をつけなくても、QScopeGuard を簡単に構築できるように提供されています。Fがラムダの場合、その型を書くことができないので、このヘルパーかクラスのテンプレート引数の推論に頼る必要があります。
使用例を以下に示します:
void myComplexCodeWithMultipleReturnPoints(int v) { // The lambda will be executed right before your function returns auto cleanup = qScopeGuard([] { code you want executed goes HERE; }); if (v == -1) return; int v2 = code_that_might_throw_exceptions(); if (v2 == -1) return; (...) }
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.