- Qt 6.11
- Qt Core
- C++ Classes
- QScopeGuard
- Qt 6.11.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`

- List of all members, including inherited members

## Public Functions

| QScopeGuard(F &&f)
| QScopeGuard(const F &f)
`(since 6.11)` void | commit()
void | dismiss()

## Related Non-Members

QScopeGuard<typename std::decay<F>::type> | qScopeGuard(F &&f)

## 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.

A scope guard can be disabled using dismiss(), in which case the function is not run at all, or executed ahead of destruction, using commit().

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

See also QScopedValueRollback.

## Member Function Documentation

### `[explicit noexcept]` QScopeGuard::QScopeGuard(F &&f)

### `[explicit noexcept]` QScopeGuard::QScopeGuard(const F &f)

Creates 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.

### `[noexcept(...), since 6.11]` void QScopeGuard::commit()

Calls the function F and then disarms the scope guard. The guard must be armed (not been dismiss()ed) when calling this function.

This function was introduced in Qt 6.11.

Note: This function is noexcept when `std::is_nothrow_invocable_v<F>` is `true`.

See also dismiss().

### `[noexcept]` void QScopeGuard::dismiss()

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

See also commit().

## Related Non-Members

### template <typename F> QScopeGuard<typename std::decay<F>::type> qScopeGuard(F &&f)

The qScopeGuard function can be used to call a function at the end of the scope.

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

This helper function is provided so that you can easily construct a QScopeGuard without having to name the template parameter for the type of the callable. If F is a lambda then you cannot write its type and relying on this helper or class template argument deduction is necessary.

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;

         //...
     }