std::has_virtual_destructor
Min standard notice:
Header: <type_traits>
std::has_virtual_destructor is a UnaryTypeTrait.
# Declarations
template< class T >
struct has_virtual_destructor;
(since C++11)
# Notes
If a class C has a public virtual destructor, it can be derived from, and the derived object can be safely deleted through a pointer to the base object (GotW #18). In this case, std::is_polymorphic
# Example
#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;
}
# Defect reports
| DR | Applied to | Behavior as published | Correct behavior |
|---|---|---|---|
| LWG 2015 | C++11 | the behavior was undefined ifT is an incomplete union type | the base characteristic isstd::false_type in this case |