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


C++ std::is_trivially_destructible用法及代碼示例

<type_traits>頭文件中提供了C++ STL的std::is_trivially_destructible模板。 C++ STL的std::is_trivially_denstructible模板用於檢查T是否是普通可破壞類型。如果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是微不足道的類型。
  • False:如果類型T不是可輕易破壞的類型。

下麵是說明C /C++中的std::is_trivially_destructible模板的程序:

程序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 
    // destructable or not 
    cout << "int:"
         << is_trivially_destructible<int>::value 
         << endl; 
  
    // Check if struct X is trivially 
    // destructable or not 
    cout << "struct X:"
         << is_trivially_destructible<X>::value 
         << endl; 
  
    // Check if struct Y is trivially 
    // destructable or not 
    cout << "struct Y:"
         << is_trivially_destructible<Y>::value 
         << endl; 
  
    // Check if struct Z is trivially 
    // destructable or not 
    cout << "struct Z:"
         << is_trivially_destructible<Z>::value 
         << endl; 
  
    // Check if class A is trivially 
    // destructable or not 
    cout << "class A:"
         << is_trivially_destructible<A>::value 
         << endl; 
  
    // Check if constructor Y(int, int) is 
    // trivially destructable 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/




相關用法


注:本文由純淨天空篩選整理自bansal_rtk_大神的英文原創作品 std::is_trivially_destructible in C++ with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。