当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ std::is_destructible用法及代码示例


<type_traits>头文件中提供了C++ STL的std::is_destructible模板。 C++ STL的std::is_destructible模板用于检查T是否可破坏。一个类称为可破坏类,其析构函数不会被删除,并且在派生类中可能会被访问​​。如果T是可销毁类型,则返回布尔值true,否则返回false。

头文件:

#include<type_traits>

模板类别:

template< class T >
struct is_destructible;

用法:

std::is_destructible<T>::value

参数:模板std::is_destructible接受单个参数T(Trait类),以检查T是否为可破坏类型。



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

  • True:如果类型T是可破坏的类型。
  • False:如果类型T不是可破坏的类型。

下面是演示C++中std::is_destructible的程序:

程序:

// 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/




相关用法


注:本文由纯净天空筛选整理自bansal_rtk_大神的英文原创作品 std::is_destructible in C++ with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。