C++ 中的 std::is_nothrow_destructible 示例
C++ STL 的std::is_nothrow_destructible模板存在于
头文件:
#include
模板类:
template
struct is_nothrow_destructible;
句法:
std::is_nothrow_destructible::value
参数:模板std::is_nothrow_destructible接受单个参数T(Trait class)来检查T是否为可破坏类型。
返回值:模板std::is_nothrow_destructible返回一个布尔值:
- True:如果类型T是可破坏类型。
- False:如果类型T不是可破坏类型。
以下是在 C++ 中演示 std::is_nothrow_destructible 的程序:
方案一:
// C++ program to illustrate
// std::is_nothrow_destructible
#include
#include
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::value
<< endl;
// Check if float is nothrow
// destructible or not
cout << "float is nothrow destructible? "
<< is_nothrow_destructible::value
<< endl;
// Check if struct X is
// nothrow destructible or not
cout << "struct X is nothrow destructible? "
<< is_nothrow_destructible::value
<< endl;
// Check if struct Y is
// nothrow destructible or not
cout << "struct Y is nothrow destructible? "
<< is_nothrow_destructible::value
<< endl;
// Check if struct Z is
// nothrow destructible or not
cout << "struct Z is nothrow destructible? "
<< is_nothrow_destructible::value
<< endl;
// Check if struct A is
// nothrow destructible or not
cout << "struct A is nothrow destructible? "
<< is_nothrow_destructible::value
<< endl;
return 0;
}
输出:
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
方案二:
// C++ program to illustrate
// std::is_nothrow_destructible
#include
#include
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::value
<< '\n';
cout << "GfG is Nothrow-destructible for GfG? "
<< is_nothrow_destructible::value
<< '\n';
cout << "GfG is Nothrow-destructible for struct X? "
<< is_nothrow_destructible::value
<< '\n';
}
输出:
GfG is Nothrow-destructible for int? true
GfG is Nothrow-destructible for GfG? true
GfG is Nothrow-destructible for struct X? true
参考: http://www.cplusplus.com/reference/type_traits/is_nothrow_destructible/