Namespaces
Variants

std:: is_destructible, std:: is_trivially_destructible, std:: is_nothrow_destructible

From cppreference.net
Metaprogramming library
Type traits
Type categories
(C++11)
(C++11) ( DR* )
Type properties
(C++11)
(C++11)
(C++14)
(C++11) (deprecated in C++26)
(C++11) ( until C++20* )
(C++11) (deprecated in C++20)
(C++11)
Type trait constants
Metafunctions
(C++17)
Supported operations
is_destructible is_trivially_destructible is_nothrow_destructible
(C++11) (C++11) (C++11)

Relationships and property queries
Type modifications
Type transformations
(C++11) (deprecated in C++23)
(C++11) (deprecated in C++23)
(C++11)
(C++11) ( until C++20* ) (C++17)

Compile-time rational arithmetic
Compile-time integer sequences
ヘッダー <type_traits> で定義
template < class T >
struct is_destructible ;
(1) (C++11以降)
template < class T >
struct is_trivially_destructible ;
(2) (C++11以降)
template < class T >
struct is_nothrow_destructible ;
(3) (C++11以降)
1) もし T が参照型の場合、メンバー定数 value true に等しいことを提供します。
T が(CV修飾されている可能性のある) void 型、関数型、または境界不明の配列である場合、 value false に等しい。
Tがオブジェクト型の場合、 U std:: remove_all_extents < T > :: type で定義される型であるとき、式 std:: declval < U & > ( ) .~U ( ) が未評価文脈で有効であれば、 value true に等しい。それ以外の場合、 value false に等しい。
2) (1) と同様であり、さらに std:: remove_all_extents < T > :: type は非クラス型、または 自明なデストラクタ を持つクラス型のいずれかである。
3) (1) と同様ですが、デストラクタは noexcept です。

T が完全型でない場合、(おそらくcv修飾された) void または不明な境界の配列である場合、動作は未定義です。

上記のテンプレートのインスタンス化が、直接的または間接的に不完全型に依存しており、その型が仮に完全化された場合に異なる結果をもたらす可能性がある場合、その動作は未定義です。

プログラムがこのページで説明されているテンプレートのいずれかに対する特殊化を追加する場合、動作は未定義です。

目次

ヘルパー変数テンプレート

template < class T >
constexpr bool is_destructible_v = is_destructible < T > :: value ;
(C++17以降)
template < class T >
constexpr bool is_trivially_destructible_v = is_trivially_destructible < T > :: value ;
(C++17以降)
template < class T >
constexpr bool is_nothrow_destructible_v = is_nothrow_destructible < T > :: value ;
(C++17以降)

std:: integral_constant から継承

メンバ定数

value
[static]
true T が破棄可能な場合、 false それ以外の場合
(公開静的メンバ定数)

メンバ関数

operator bool
オブジェクトを bool に変換し、 value を返す
(公開メンバ関数)
operator()
(C++14)
value を返す
(公開メンバ関数)

メンバ型

定義
value_type bool
type std:: integral_constant < bool , value >

注記

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)
// Not real C++. Shall P2996 be approved, the following implementation will be available:
template<typename t>
struct is_trivially_destructible
     : std::integral_constant<bool, std::meta::type_is_trivially_destructible(^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 <iostream>
#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 仮想的なラップ構造体のため仕様が完了不可能であった 完了された

関連項目

特定の引数に対するコンストラクタが型に存在するかチェックする
(クラステンプレート)
型が仮想デストラクタを持つかチェックする
(クラステンプレート)
型のオブジェクトが破棄可能であることを指定する
(コンセプト)
destructor 確保したリソースを解放する