Skip to content
Related Articles

Related Articles

std::is_trivially_destructible in C++ with Examples
  • Last Updated : 12 Jun, 2020
GeeksforGeeks - Summer Carnival Banner

The std::is_trivially_destructible template of C++ STL is present in the <type_traits> header file. The std::is_trivially_denstructible template of C++ STL is used to check whether T is trivially destructible type or not. It return the boolean value true if T is trivially destructible type Otherwise return false.

Header File:

#include<type_traits>

Template Class:

template <class T>
struct is_trivially_destructible;

Syntax:

std::is_trivially_destructible<T>::value

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



Return Value: This template returns a boolean variable as shown below:

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

Below is the program to illustrates the std::is_trivially_destructible template in C/C++:

Program 1:




// C++ program to illustrate
// std::is_trivially_destructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Declare structures
struct Y {
    // Constructor
    Y(int, int){};
};
  
struct X {
  
    // Destructor
    ~X() noexcept(false)
    {
    }
};
  
struct Z {
    ~Z() = default;
};
  
// Declare classes
class A {
    virtual void fn() {}
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    // Check if int is trivially
    // destructable or not
    cout << "int: "
         << is_trivially_destructible<int>::value
         << endl;
  
    // Check if struct X is trivially
    // destructable or not
    cout << "struct X: "
         << is_trivially_destructible<X>::value
         << endl;
  
    // Check if struct Y is trivially
    // destructable or not
    cout << "struct Y: "
         << is_trivially_destructible<Y>::value
         << endl;
  
    // Check if struct Z is trivially
    // destructable or not
    cout << "struct Z: "
         << is_trivially_destructible<Z>::value
         << endl;
  
    // Check if class A is trivially
    // destructable or not
    cout << "class A: "
         << is_trivially_destructible<A>::value
         << endl;
  
    // Check if constructor Y(int, int) is
    // trivially destructable or not
    cout << "Constructor Y(int, int): "
         << is_trivially_destructible<Y(int, int)>::value
         << endl;
  
    return 0;
}
Output:
int: true
struct X: false
struct Y: true
struct Z: true
class A: true
Constructor Y(int, int): false

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

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.
My Personal Notes arrow_drop_up
Recommended Articles
Page :