C++中的std::is_destructible用法示例

2021年4月29日18:34:06 发表评论 791 次浏览

std::is_destructible的模板C++ STL存在于<type_traits>头文件。的std::is_destructibleC++ STL模板用于检查是否Ť是否可破坏。一种类称为可破坏的, 其析构函数不会被删除并且在派生类中可能是可访问的。如果满足以下条件, 则返回布尔值true:Ť是可破坏的类型, 否则返回false。

头文件:

#include<type_traits>

模板类别:

template<class T>
struct is_destructible;

语法如下:

std::is_destructible<T>::value

参数:模板std::is_destructible接受一个参数T(特质类)检查是否Ť是否为可破坏类型。

返回值:模板std::is_destructible返回一个布尔变量, 如下所示:

  • true:如果类型T是可破坏的类型。
  • false:如果类型T不是可破坏的类型。

以下是演示程序std::is_destructible在C++中:

程序:

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

输出如下:

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

参考: http://www.cplusplus.com/reference/type_traits/is_destructible/

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C++ STL通过激烈的问题解决过程来训练和掌握这些概念。


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: