📜  C++ 中的 std::is_nothrow_destructible 示例

📅  最后修改于: 2022-05-13 01:57:14.664000             🧑  作者: Mango

C++ 中的 std::is_nothrow_destructible 示例

C++ STL 的std::is_nothrow_destructible模板存在于 头文件中。 C++ STL 的std::is_nothrow_denstructible模板用于检查 T是否为可破坏类型,以不抛出任何异常而闻名。如果 T是可破坏类型,则返回布尔值 true,否则返回 false。

头文件:

#include

模板类:

template 
struct is_nothrow_destructible;

句法:

std::is_nothrow_destructible::value

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



返回值:模板std::is_nothrow_destructible返回一个布尔值:

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

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

方案一:

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

方案二:

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