QScopeGuard Class | Qt Core 6.4.1

- Qt 6.4
- Qt Core
- C++ Classes
- QScopeGuard
- Qt 6.4.1 Reference Documentation

# QScopeGuard Class

template <typename F> class QScopeGuard

Provides a scope guard for calling a function at the end of a scope. More...

Header: | #include <QScopeGuard>
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: | QT += core
Since: | Qt 5.12

- List of all members, including inherited members

## Public Functions

| QScopeGuard(const F &f)
| QScopeGuard(F &&f)
void | dismiss()

## Detailed Description

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.

Note: Exceptions are not supported. The callable shouldn't throw when executed, copied or moved.

See also QScopedValueRollback.

## Member Function Documentation

### `[since 5.15]` QScopeGuard::QScopeGuard(F &&f)

### `[since 5.15]` QScopeGuard::QScopeGuard(const F &f)

Create a scope guard that will execute f at the end of the scope.

If F is a lambda, its type cannot be written. In that case you need to either rely on class template argument deduction (C++17 feature) and leave the template parameter out completely or use the helper function qScopeGuard() instead of this constructor.

This function was introduced in Qt 5.15.

### void QScopeGuard::dismiss()

Disarms the scope guard, so that the function F will not be called at the end of the scope.