std::is_nothrow_destructible in C++ with Examples
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:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
struct X {
};
struct Y {
~Y() = delete ;
};
struct Z {
~Z() = default ;
};
struct A : Y {
};
int main()
{
cout << boolalpha;
cout << "int is nothrow destructible? "
<< is_nothrow_destructible< int >::value
<< endl;
cout << "float is nothrow destructible? "
<< is_nothrow_destructible< float >::value
<< endl;
cout << "struct X is nothrow destructible? "
<< is_nothrow_destructible<X>::value
<< endl;
cout << "struct Y is nothrow destructible? "
<< is_nothrow_destructible<Y>::value
<< endl;
cout << "struct Z is nothrow destructible? "
<< is_nothrow_destructible<Z>::value
<< endl;
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:
#include <bits/stdc++.h>
#include <type_traits>
using namespace std;
class GfG {
int v1;
float v2;
public :
GfG( int n)
: v1(n), v2()
{
}
GfG( int n, double f) noexcept
: v1(n),
v2(f) {}
};
struct X {
int n;
X() = default ;
};
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
Share your thoughts in the comments
Please Login to comment...