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 함수를 실행하는 것이 유일한 목적인 클래스입니다. 함수가 정상적으로 종료되든, 반환문에 의해 조기 종료되든, 예외에 의해 종료되든 관계없이 정리 코드가 실행되도록 보장하는 데 유용합니다.

참고: 예외는 지원되지 않습니다. 호출 가능 함수는 실행, 복사 또는 이동할 때 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.