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) | |
(since 6.11) void | commit() |
| void | dismiss() |
관련 비멤버
| QScopeGuard<typename std::decay<F>::type> | qScopeGuard(F &&f) |
상세 설명
QScopeGuard<F>는 템플릿 클래스로, 여기서 F 는 소멸자에서 호출될 호출 가능 객체(함수 포인터, 펑터, 람다 등)의 유형입니다. 이는 함수가 정상적으로 종료되든, return 문에 의해 조기에 종료되든, 예외로 인해 종료되든 상관없이 정리 코드가 실행되도록 보장하는 데 유용합니다.
스코프 가드는 dismiss()을 사용하여 비활성화할 수 있으며, 이 경우 함수는 전혀 실행되지 않습니다. 또는 commit()을 사용하여 소멸 전에 함수를 실행할 수도 있습니다.
참고: 예외는 지원되지 않습니다. 호출 가능 객체는 실행, 복사 또는 이동 시 예외를 발생시켜서는 안 됩니다.
QScopedValueRollback도 참조하십시오 .
멤버 함수 문서
[explicit noexcept] QScopeGuard::QScopeGuard(F &&f)
[explicit noexcept] QScopeGuard::QScopeGuard(const F &f)
범위 끝에서 ` f `를 실행하는 범위 가드를 생성합니다.
F가 람다인 경우, 그 타입을 명시할 수 없습니다. 이 경우 클래스 템플릿 인자 추론(C++17 기능)을 활용하여 템플릿 매개변수를 완전히 생략하거나, 이 생성자 대신 헬퍼 함수 qScopeGuard()을 사용해야 합니다.
[noexcept(...), since 6.11] void QScopeGuard::commit()
함수 F를 호출한 다음, 범위 가드를 해제합니다. 이 함수를 호출할 때는 가드가 활성화된 상태여야 하며( dismiss()가 호출되지 않은 상태여야 함),
이 함수는 Qt 6.11에서 도입되었습니다.
참고: std::is_nothrow_invocable_v<F> 가 ` true`인 경우,이 함수는 `noexcept`입니다.
dismiss()도 참조하십시오 .
[noexcept] void QScopeGuard::dismiss()
스코프 가드를 해제하여, 스코프가 끝날 때 함수 F가 호출되지 않도록 합니다.
commit()도 참조하십시오 .
관련 비멤버 함수
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;
//...
}© 2026 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.