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


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


<type_traits>頭文件中提供了C++ STL的std::is_nothrow_destructible模板。 C++ STL的std::is_nothrow_denstructible模板用於檢查T是否為可破壞類型,並且眾所周知它不會引發任何異常。如果T是可銷毀類型,則返回布爾值true,否則返回false。

頭文件:

#include<type_traits>

模板類別:

template <class T>
struct is_nothrow_destructible;

用法:

std::is_nothrow_destructible<T>::value

參數:模板std::is_nothrow_destructible接受單個參數T(特質類)以檢查T是否為可破壞類型。



返回值:模板std::is_nothrow_destructible返回一個布爾值:

  • True:如果類型T是可破壞的類型。
  • False:如果類型T是不可破壞的類型。

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

程序1:

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

程序2:

// C++ program to illustrate 
// std::is_nothrow_destructible 
#include <bits/stdc++.h> 
#include <type_traits> 
using namespace std; 
  
// Class GfG 
class GfG { 
    int v1; 
    float v2; 
  
public:
    GfG(int n) 
        :v1(n), v2() 
    { 
    } 
    GfG(int n, double f) noexcept 
        :v1(n), 
          v2(f) {} 
}; 
  
// Declare Structure 
  
struct X { 
    int n; 
    X() = default; 
}; 
  
// Driver Code 
int main() 
{ 
  
    cout << boolalpha; 
  
    cout << "GfG is Nothrow-destructible for int? "
         << is_nothrow_destructible<int>::value 
         << '\n'; 
  
    cout << "GfG is Nothrow-destructible for GfG? "
         << is_nothrow_destructible<GfG>::value 
         << '\n'; 
  
    cout << "GfG is Nothrow-destructible for struct X? "
         << is_nothrow_destructible<X>::value 
         << '\n'; 
}
輸出:
GfG is Nothrow-destructible for int? true
GfG is Nothrow-destructible for GfG? true
GfG is Nothrow-destructible for struct X? true

參考: http://www.cplusplus.com/reference/type_traits/is_nothrow_destructible/




相關用法


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