The std::has_virtual_destructor of C++ STL is used to check whether the given type T has a virtual destructor or not. It returns the boolean value either true or false. Below is the syntax for the same:
Header File:
#include<type_traits>
Syntax:
template <class T> struct has_virtual_destructor;
Parameter: The template std::has_virtual_destructor accepts a single parameter T (Trait class) to check whether T has a virtual destructor or not.
Return Values:
- True: If virtual destructor present.
- False: If virtual destructor not present.
Below programs illustrate the std::has_virtual_destructor template in C++ STL:
Program 1:
// C++ program to illustrate // has_virtual_destructor example #include <bits/stdc++.h> #include <type_traits> using namespace std;
struct gfg1 {
}; struct gfg2 {
virtual ~gfg2() {}
}; struct gfg3 : gfg2 {
}; // Driver Code int main()
{ cout << boolalpha;
cout << "has_virtual_destructor:"
<< endl;
cout << "int: "
<< has_virtual_destructor< int >::value
<< endl;
cout << "gfg1: "
<< has_virtual_destructor<gfg1>::value
<< endl;
cout << "gfg2: "
<< has_virtual_destructor<gfg2>::value
<< endl;
cout << "gfg3: "
<< has_virtual_destructor<gfg3>::value
<< endl;
return 0;
} |
has_virtual_destructor: int: false gfg1: false gfg2: true gfg3: true
Program 2:
// C++ program to illustrate // has_virtual_destructor example #include <bits/stdc++.h> #include <type_traits> using namespace std;
struct gfg1 {
virtual ~gfg1() {}
}; struct gfg2 {
}; struct gfg3 : gfg1 {
}; // Driver Code int main()
{ cout << boolalpha;
cout << "has_virtual_destructor:"
<< endl;
cout << "int: "
<< has_virtual_destructor< int >::value
<< endl;
cout << "gfg1: "
<< has_virtual_destructor<gfg1>::value
<< endl;
cout << "gfg2: "
<< has_virtual_destructor<gfg2>::value
<< endl;
cout << "gfg3: "
<< has_virtual_destructor<gfg3>::value
<< endl;
return 0;
} |
has_virtual_destructor: int: false gfg1: true gfg2: false gfg3: true
Reference: http://www.cplusplus.com/reference/type_traits/has_virtual_destructor/