std::has_virtual_destructor
来自 cppreference.cn
定义于头文件 <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) |
检查类型是否为多态类类型 (类模板) |