Understanding std::is_destructible in C++: A Practical Usage Guide

std::is_destructible template C++ STL exists in <type_traits> header file. The std::is_destructibleC++ STL template is used to check if it is destructible. One class is called destructible, and its destructor is not removed and may be accessible in the derived class. A boolean value is returned true if the following conditions are met: T is a destructible type, otherwise false is returned.

Header Files:

#include<type_traits>

Template Categories:

template<class T>
struct is_destructible;

The syntax is as follows:

std::is_destructible<T>::value

Parameter: Template std::is_destructible Accept a parameter T (trait class) to check if T is destructible.

The return value: the template std::is_destructible returns a Boolean variable, as follows:

  • true: If type T is destructible.
  • false: If type T is not destructible.

Here’s the demo program std::is_destructible in C++:

Procedure:

//C++ program to illustrate
//std::is_destructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
//Declare a structures
struct X {
};
  
struct Y {
  
     //Destructors
     ~Y() = delete ;
};
  
struct Z {
     ~Z() = default ;
};
  
struct A : Y {
};
  
//Driver Code
int main()
{
  
     cout <<boolalpha;
  
     //Check if int is destructible
     //or not
     cout <<"int is destructible? "
          <<is_destructible<int>::value
          <<endl;
  
     //Check if float is destructible
     //or not
     cout <<"float is destructible? "
          <<is_destructible<float>::value
          <<endl;
  
     //Check if struct X is
     //destructible or not
     cout <<"struct X is destructible? "
          <<is_destructible<X>::value
          <<endl;
  
     //Check if struct Y is
     //destructible or not
     cout <<"struct Y is destructible? "
          <<is_destructible<Y>::value
          <<endl;
  
     //Check if struct Z is
     //destructible or not
     cout <<"struct Z is destructible? "
          <<is_destructible<Z>::value
          <<endl;
  
     //Check if struct A is
     //destructible or not
     cout <<"struct A is destructible? "
          <<is_destructible<A>::value
          <<endl;
  
     return 0;
}

The output is as follows:

int is destructible? true
float is destructible? true
struct X is destructible? true
struct Y is destructible? false
struct Z is destructible? true
struct A is destructible? false

References: http://www.cplusplus.com/reference/type_traits/is_destructible/

Considered one of the most sought-after skills in the industry, we have our own coding foundation, C++ STL, to train and master these concepts through an intense problem-solving process.