std:: is_destructible, std:: is_trivially_destructible, std:: is_nothrow_destructible
From cppreference.net
|
ヘッダー
<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
に等しい。
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 | 仮想的なラップ構造体のため仕様が完了不可能であった | 完了された |
関連項目
|
(C++11)
(C++11)
(C++11)
|
特定の引数に対するコンストラクタが型に存在するかチェックする
(クラステンプレート) |
|
(C++11)
|
型が仮想デストラクタを持つかチェックする
(クラステンプレート) |
|
(C++20)
|
型のオブジェクトが破棄可能であることを指定する
(コンセプト) |
| destructor | 確保したリソースを解放する |