std::has_virtual_destructor in C++ with Examples
Last Updated :
13 Jul, 2021
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:
CPP14
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
struct gfg1 {
};
struct gfg2 {
virtual ~gfg2() {}
};
struct gfg3 : gfg2 {
};
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:
CPP14
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
struct gfg1 {
virtual ~gfg1() {}
};
struct gfg2 {
};
struct gfg3 : gfg1 {
};
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/
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...