Related Articles

Related Articles

std::is_destructible in C++ with Example
  • Last Updated : 12 Jun, 2020

The std::is_destructible template of C++ STL is present in the <type_traits> header file. The std::is_destructible template of C++ STL is used to check whether the T is destructible or not. A class is called destructible whose destructor is not deleted and potentially accessible in derived classes. It return the boolean value true if T is destructible type, otherwise return false.

Header File:

#include<type_traits>

Template Class:

template< class T >
struct is_destructible;

Syntax:

std::is_destructible<T>::value

Parameter: The template std::is_destructible accepts a single parameter T(Trait class) to check whether T is destructible type or not.



Return Value: The template std::is_destructible returns a boolean variable as shown below:

  • True: If the type T is a destructible type.
  • False: If the type T is not a destructible type.

Below are the programs to demonstrate std::is_destructible in C++:

Program:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

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

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

Rated as one of the most sought after skills in the industry, own the basics of coding with our C++ STL Course and master the very concepts by intense problem-solving.




My Personal Notes arrow_drop_up
Recommended Articles
Page :