📌  相关文章
📜  C++中带有示例的std :: is_trivially_destructible

📅  最后修改于: 2021-05-30 13:19:44             🧑  作者: Mango

< type_traits >头文件中提供了C++ STL的std :: is_trivially_destructible模板。 C++ STL的std :: is_trivially_denstructible模板用于检查T是否为普通可破坏类型。如果T是普通可破坏类型,则它返回布尔值true,否则返回false。

头文件:

#include

模板类别:

template 
struct is_trivially_destructible;

句法:

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

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”