std::experimental:: scope_exit
|
헤더 파일에 정의됨
<experimental/scope>
|
||
|
template
<
class
EF
>
class scope_exit ; |
(라이브러리 펀더멘털 TS v3) | |
클래스 템플릿
scope_exit
는 스코프가 종료될 때 종료 함수를 호출하도록 설계된 범용 스코프 가드입니다.
scope_exit
는
CopyConstructible
,
CopyAssignable
또는
MoveAssignable
요구 사항을 만족하지 않습니다. 그러나
EF
가 특정 요구 사항을 충족하는 경우
MoveConstructible
일 수 있으며, 이는
scope_exit
를 다른 객체로 감싸는 것을 허용합니다.
A
scope_exit
는 활성 상태, 즉 소멸 시 종료 함수를 호출하거나, 비활성 상태, 즉 소멸 시 아무 작업도 수행하지 않을 수 있습니다.
scope_exit
는 종료 함수로부터 생성된 후 활성 상태입니다.
scope_exit
는 수동으로 또는 자동으로(이동 생성자에 의해)
release()
를 호출하여 비활성화될 수 있습니다. 비활성
scope_exit
는 다른 비활성
scope_exit
로 초기화하여 얻을 수도 있습니다. 일단
scope_exit
가 비활성화되면 다시 활성화될 수 없습니다.
A
scope_exit
는 효과적으로
EF
와 활성 상태를 나타내는
bool
플래그를 보유합니다.
목차 |
템플릿 매개변수
| EF | - | 저장된 종료 함수의 유형 |
| 유형 요구사항 | ||
-
EF
는 다음 중 하나여야 합니다:
|
||
|
-
|
||
멤버 함수
새로운
scope_exit
을 생성합니다
(public member function) |
|
scope_exit
이 활성 상태일 때 스코프를 벗어나면 exit 함수를 호출한 후
scope_exit
을 파괴합니다
(public member function) |
|
|
operator=
[deleted]
|
scope_exit
은 할당할 수 없음
(public member function) |
Modifiers |
|
scope_exit
을 비활성 상태로 만듭니다
(public member function) |
|
추론 가이드
참고 사항
동적 저장 기간의
scope_exit
를 생성하면 예상치 못한 동작이 발생할 수 있습니다.
만약
EF
가 정의된 함수의 지역 변수를 참조하는 경우, 예를 들어 해당 변수를 참조로 캡처하는 람다로, 그리고 그 변수가 해당 함수의 반환 피연산자로 사용되는 경우,
scope_exit
객체에 저장된
EF
의 소멸자가 실행되어 exit 함수를 호출할 때, 그 변수는 이미 반환되었을 수 있습니다. 이는 예상치 못한 동작을 초래할 수 있습니다.
예제
#include <iostream> #include <cstdlib> #include <string_view> #include <experimental/scope> void print_exit_status(std::string_view name, bool exit_status, bool did_throw) { std::cout << name << ":\n"; std::cout << " Throwed exception " << (did_throw ? "yes" : "no") << "\n"; std::cout << " Exit status " << (exit_status ? "finished" : "pending") << "\n\n"; } // 무작위로 예외를 발생시킴 (50% 확률) void maybe_throw() { if (std::rand() >= RAND_MAX / 2) throw std::exception{}; } int main() { bool exit_status{false}, did_throw{false}; // "스코프 종료 시" 수동 처리 try { maybe_throw(); exit_status = true; } catch (...) { did_throw = true; } print_exit_status("Manual handling", exit_status, did_throw); // scope_exit 사용: 스코프 종료 시 실행 (성공 또는 예외 발생 시) exit_status = did_throw = false; try { auto guard = std::experimental::scope_exit{[&]{ exit_status = true; } }; maybe_throw(); } catch (...) { did_throw = true; } print_exit_status("scope_exit", exit_status, did_throw); // scope_fail 사용: 예외 발생 시에만 실행 exit_status = did_throw = false; try { auto guard = std::experimental::scope_fail{[&]{ exit_status = true; } }; maybe_throw(); } catch (...) { did_throw = true; } print_exit_status("scope_fail", exit_status, did_throw); // scope_success 사용: 예외가 발생하지 않을 때만 실행 exit_status = did_throw = false; try { auto guard = std::experimental::scope_success{[&]{ exit_status = true; } }; maybe_throw(); } catch (...) { did_throw = true; } print_exit_status("scope_success", exit_status, did_throw); }
출력:
Manual handling: Throwed exception yes Exit status pending scope_exit: Throwed exception no Exit status finished scope_fail: Throwed exception yes Exit status finished scope_success: Throwed exception yes Exit status pending
참고 항목
|
함수 객체를 감싸고 스코프를 벗어날 때 예외를 통해 호출합니다
(class template) |
|
|
함수 객체를 감싸고 스코프를 정상적으로 벗어날 때 호출합니다
(class template) |
|
|
(C++11)
|
unique_ptr
을 위한 기본 삭제자
(class template) |