QScopeGuard Class
Provides a scope guard for calling a function at the end of a scope. More...
Header: | #include <QScopeGuard> |
qmake: | QT += core |
Since: | Qt 5.12 |
This class was introduced in Qt 5.12.
Public Functions
void | dismiss() |
Related Non-Members
QScopeGuard<F> | qScopeGuard(F f) |
Member Function Documentation
void QScopeGuard::dismiss()
Disarms the scope guard, so that the function F will not be called at the end of the scope.
Related Non-Members
QScopeGuard<F> qScopeGuard(F f)
The qScopeGuard function can be used to call a function at the end of the scope.
QScopeGuard<F> is a class of which the sole purpose is to run the function f in its destructor. This is useful for guaranteeing your cleanup code is executed, whether the function is exited normally, exited early by a return statement, or exited by an exception.
If F is a lambda then you cannot instantiate the template directly, therefore the qScopeGuard() helper is provided and QScopeGuard<F> is made a private implementation detail.
Example usage is as follows:
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; (...) }
Note: Exceptions are not supported. The callable shouldn't throw when executed, copied or moved.
See also QScopedValueRollback.
© 2020 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.