std::has_virtual_destructor

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::value is true.

# 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

DRApplied toBehavior as publishedCorrect behavior
LWG 2015C++11the behavior was undefined ifT is an incomplete union typethe base characteristic isstd::false_type in this case

# See also