C++中的std::is_trivially_destructible和示例
C++ STL中的 std::is_trivially_destructible 模板位于 < type_traits>头文件中。C++ STL中的 std::is_trivially_destructible 模板用于检查 T 是否为平凡析构类型。如果是,则返回布尔值true,否则返回false。 头文件:
#include<type_traits>
模板类:
template<class T>
struct is_trivially_destructible;
语法:
std::is_trivially_destructible<T>::value
参数: 模板 std::is_trivially_destructible 接受一个单一的参数 T(Trait类) ,用于检查 T 是否为平凡析构类型。 返回值: 此模板将返回以下布尔变量:
- 如果类型 T 是平凡析构类型,则为true。
- 如果类型 T 不是平凡析构类型,则为false。
以下是展示 std::is_trivially_destructible模板 C/C++示例的程序:
程序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
// destructible or not
cout << "int: "
<< is_trivially_destructible<int>::value
<< endl;
// Check if struct X is trivially
// destructible or not
cout << "struct X: "
<< is_trivially_destructible<X>::value
<< endl;
// Check if struct Y is trivially
// destructible or not
cout << "struct Y: "
<< is_trivially_destructible<Y>::value
<< endl;
// Check if struct Z is trivially
// destructible or not
cout << "struct Z: "
<< is_trivially_destructible<Z>::value
<< endl;
// Check if class A is trivially
// destructible or not
cout << "class A: "
<< is_trivially_destructible<A>::value
<< endl;
// Check if constructor Y(int, int) is
// trivially destructible or not
cout << "Constructor Y(int, int): "
<< is_trivially_destructible<Y(int, int)>::value
<< endl;
return 0;
}
输出:
int: true
struct X: false
struct Y: true
struct Z: true
class A: true
Constructor Y(int, int): false
参考文献: http://www.cplusplus.com/reference/type_traits/is_trivially_destructible/