Open In App

std::is_nothrow_destructible in C++ with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

The std::is_nothrow_destructible template of C++ STL is present in the <type_traits> header file. The std::is_nothrow_denstructible template of C++ STL is used to check whether T is destructible type or not and this is known for not to throw any exception. 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_nothrow_destructible;

Syntax:

std::is_nothrow_destructible<T>::value

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

Return Value: The template std::is_nothrow_destructible returns a boolean value:

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

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

Program 1:




// C++ program to illustrate
// std::is_nothrow_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 nothrow
    // destructible or not
    cout << "int is nothrow destructible? "
         << is_nothrow_destructible<int>::value
         << endl;
  
    // Check if float is nothrow
    // destructible or not
    cout << "float is nothrow destructible? "
         << is_nothrow_destructible<float>::value
         << endl;
  
    // Check if struct X is
    // nothrow destructible or not
    cout << "struct X is nothrow destructible? "
         << is_nothrow_destructible<X>::value
         << endl;
  
    // Check if struct Y is
    // nothrow destructible or not
    cout << "struct Y is nothrow destructible? "
         << is_nothrow_destructible<Y>::value
         << endl;
  
    // Check if struct Z is
    // nothrow destructible or not
    cout << "struct Z is nothrow destructible? "
         << is_nothrow_destructible<Z>::value
         << endl;
  
    // Check if struct A is
    // nothrow destructible or not
    cout << "struct A is nothrow destructible? "
         << is_nothrow_destructible<A>::value
         << endl;
  
    return 0;
}


Output:

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

Program 2:




// C++ program to illustrate
// std::is_nothrow_destructible
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
  
// Class GfG
class GfG {
    int v1;
    float v2;
  
public:
    GfG(int n)
        : v1(n), v2()
    {
    }
    GfG(int n, double f) noexcept
        : v1(n),
          v2(f) {}
};
  
// Declare Structure
  
struct X {
    int n;
    X() = default;
};
  
// Driver Code
int main()
{
  
    cout << boolalpha;
  
    cout << "GfG is Nothrow-destructible for int? "
         << is_nothrow_destructible<int>::value
         << '\n';
  
    cout << "GfG is Nothrow-destructible for GfG? "
         << is_nothrow_destructible<GfG>::value
         << '\n';
  
    cout << "GfG is Nothrow-destructible for struct X? "
         << is_nothrow_destructible<X>::value
         << '\n';
}


Output:

GfG is Nothrow-destructible for int? true
GfG is Nothrow-destructible for GfG? true
GfG is Nothrow-destructible for struct X? true

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



Last Updated : 24 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads