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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

has_virtual_destructor:
int: false
gfg1: false
gfg2: true
gfg3: true

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

has_virtual_destructor:
int: false
gfg1: true
gfg2: false
gfg3: true

Reference: http://www.cplusplus.com/reference/type_traits/has_virtual_destructor/

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.