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


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。非经特殊声明,原始代码版权归原作者所有,本译文的传播和使用请遵循“署名-相同方式共享 4.0 国际 (CC BY-SA 4.0)”协议。