std::has_virtual_destructor
來自 cppreference.com
定義於標頭檔案 <type_traits> |
||
template< class T > struct has_virtual_destructor; |
(C++11 起) | |
std::has_virtual_destructor
是一個 一元型別特性。
如果 T
是一個具有虛解構函式的型別,則基特徵為 std::true_type。對於任何其他型別,基特徵為 std::false_type。
若 T
是不完整非聯合類型別,則行為未定義。
如果程式為 std::has_virtual_destructor
或 std::has_virtual_destructor_v
新增特化,則行為是未定義的。
目錄 |
[編輯] 模板引數
T | - | 要檢查的型別 |
[編輯] 輔助變數模板
template< class T > constexpr bool has_virtual_destructor_v = has_virtual_destructor<T>::value; |
(C++17 起) | |
繼承自 std::integral_constant
成員常量
value [靜態] |
如果 T 有虛解構函式,則為 true,否則為 false。(public static 成員常量) |
成員函式
operator bool |
將物件轉換為 bool,返回 value (公開成員函式) |
operator() (C++14) |
返回 value (公開成員函式) |
成員型別
型別 | 定義 |
value_type
|
bool |
型別
|
std::integral_constant<bool, value> |
[編輯] 備註
如果類 C
具有公共虛解構函式,它可以被派生,並且派生物件可以透過指向基物件的指標安全地刪除(GotW #18)。在這種情況下,std::is_polymorphic<C>::value 為 true。
[編輯] 示例
執行此程式碼
#include <type_traits> struct S {}; static_assert(!std::has_virtual_destructor_v<S>); struct B { virtual ~B() {} }; static_assert(std::has_virtual_destructor_v<B>); struct D : B { ~D() {} }; static_assert(std::has_virtual_destructor_v<D>); int main() { B* pd = new D; delete pd; }
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 2015 | C++11 | 若T 是不完整聯合型別 |
基特徵是 在這種情況下為 std::false_type |
[編輯] 另請參見
(C++11)(C++11)(C++11) |
檢查型別是否具有非刪除的解構函式 (類模板) |
(C++11) |
檢查型別是否為多型類型別 (類模板) |