std::has_virtual_destructor in C++ with Examples
The std::has_virtual_destructor of C++ STL is used to check whether the given type T has a vitual 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; } |
Output:
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; } |
Output:
has_virtual_destructor: int: false gfg1: true gfg2: false gfg3: true
Reference: http://www.cplusplus.com/reference/type_traits/has_virtual_destructor/
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.