types_is_destructible
types_is_destructible (파괴 가능성 검사)
이 페이지에서는 C++ 표준 라이브러리의 is_destructible, is_trivially_destructible, is_nothrow_destructible 형식 특성에 대해 다뤄요. 이 특성들은 컴파일 타임에 어떤 형식이 파괴 가능한지, 사소하게 파괴 가능한지, 예외 없이 파괴 가능한지 검사해 줘요.
출처: cppreference
본문
<type_traits> 헤더에 정의되어 있어요.
| <type_traits> 헤더에 정의됨 | ||
|---|---|---|
| template < class T > struct is_destructible ; | (1) | (since C++11) |
| template < class T > struct is_trivially_destructible ; | (2) | (since C++11) |
| template < class T > struct is_nothrow_destructible ; | (3) | (since C++11) |
만약 T가 완전한 형식(complete type)이 아니거나, (cv 한정된) void이거나, 경계를 알 수 없는 배열이라면 동작이 정의되지 않아요.
위 템플릿 중 하나의 인스턴스화가 불완전한 형식에 직간접적으로 의존하고, 그 형식이 가상적으로 완성되었을 때 결과가 달라질 수 있다면 동작이 정의되지 않아요.
이 페이지에 설명된 템플릿 중 하나에 프로그램이 특수화를 추가하면 동작이 정의되지 않아요.
헬퍼 변수 템플릿
| template < class T > constexpr bool is_destructible_v = is_destructible < T >:: value ; | (since C++17) | |
|---|---|---|
| template < class T > constexpr bool is_trivially_destructible_v = is_trivially_destructible < T >:: value ; | (since C++17) | |
| template < class T > constexpr bool is_nothrow_destructible_v = is_nothrow_destructible < T >:: value ; | (since C++17) |
std::integral_constant에서 상속됨
멤버 상수
| value [static] | T가 파괴 가능하면 true, 아니면 false (공용 정적 멤버 상수) |
|---|
멤버 함수
| operator bool | 객체를 bool로 변환하고 value를 반환해요 (공용 멤버 함수) |
|---|---|
| operator() (C++14) | value를 반환해요 (공용 멤버 함수) |
멤버 형식
| 형식 | 정의 |
|---|---|
| value_type | bool |
| type | std :: integral_constant < bool , value > |
참고 사항
소멸자가 스택 풀기(stack unwinding) 중에 예외를 던지면 C++ 프로그램이 종료되기 때문에(보통 예측할 수 없어요), 모든 실용적인 소멸자는 noexcept로 선언되지 않았더라도 예외를 던지지 않아요. C++ 표준 라이브러리에 있는 모든 소멸자는 예외를 던지지 않아요.
사소하게 파괴 가능한(trivially destructible) 객체가 차지하는 저장 공간은 소멸자를 호출하지 않고 재사용할 수 있어요.
가능한 구현
| is_destructible (1) |
|---|
| // C++20 required template < typename T > struct is_destructible : std :: integral_constant < bool , requires ( T object ) { object . ~ T (); } > {}; |
| is_trivially_destructible (2) |
| // C++26 required template < typename T > struct is_trivially_destructible : std :: integral_constant < bool , std :: meta :: is_trivially_destructible_type ( ^^ T ) > {}; |
| is_nothrow_destructible (3) |
| // C++20 required template < typename T > struct is_nothrow_destructible : std :: integral_constant < bool , requires ( T object ) { { object . ~ T ()} noexcept ; } > {}; |
예제
#include <string>
#include <type_traits>
struct Foo
{
std::string str;
~Foo() noexcept {};
};
struct Bar
{
~Bar() = default;
};
static_assert(std::is_destructible<std::string>::value == true);
static_assert(std::is_trivially_destructible_v<Foo> == false);
static_assert(std::is_nothrow_destructible<Foo>() == true);
static_assert(std::is_trivially_destructible<Bar>{} == true);
int main() {}
결함 보고
다음 동작 변경 결함 보고서는 이전에 발표된 C++ 표준에 소급 적용되었어요.
| DR | 적용 대상 | 발표된 동작 | 올바른 동작 |
|---|---|---|---|
| LWG 2049 | C++11 | 가상의 래핑 구조체 때문에 명세가 완성될 수 없었음 | 완성됨 |
같이 보기
| is_constructible is_trivially_constructible is_nothrow_constructible (C++11) (C++11) (C++11) | 특정 인자에 대한 생성자가 있는지 검사해요 (클래스 템플릿) [edit] |
|---|---|
| has_virtual_destructor (C++11) | 가상 소멸자가 있는지 검사해요 (클래스 템플릿) [edit] |
| destructible (C++20) | 해당 형식의 객체를 파괴할 수 있음을 명시해요 (컨셉) [edit] |
| destructor | 점유한 자원을 해제해요 [edit] |
| is_destructible_type is_trivially_destructible_type is_nothrow_destructible_type (C++26) (C++26) (C++26) | 반영된 형식에 삭제되지 않은 소멸자가 있는지 검사해요 (함수) [edit] |