當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。