C/C++|std::is_trivially_destructible

std::is_trivially_destructible来判断 一个 内置类型或structure/class/union类型 是否为 “容易销毁的(trivially destructible)”,从字面意思也可以看出,主要是check这个类型的 析构函数。官网给出如下解释:
A trivially destructible class is a class (defined with class, struct or union) that:

  1. uses the implicitly defined destructor.
  2. the destructor is not virtual.
  3. its base class and non-static data members (if any) are themselves also trivially destructible types.
也就是说,如果一个类定义了非默认的析构函数,比如类的构造函数中有new,那么需要自写析构来delete,那么std::is_trivially_destructible::value将返回false。
如果一个类中的 非静态成员变量 是 not rivially destructible types,那么 这个类 也将是not rivially destructible types,std::is_trivially_destructible::value将返回false。
在SystemC中,对于sc_bv 类型,由于其基类sc_bv_base有自定义析构函数,所以std::is_trivially_destructible>::value 返回false。
virtual ~sc_bv_base() { delete [] m_data; }

参考:
is_trivially_destructible - C++ Reference
C++ std::is_trivially_destructible用法及代码示例 - 纯净天空
STL in C++11 (Allocator 2) - 简书
// execute: //g++ -g -Wall -lsystemc -m64 -pthread main.cpp //-L/$(your systemc path)/lib-linux64 //-I/$(your systemc path)/include-I/$(your systemc path)/src/tlm_utils -o sim#include // C++ program to illustrate // std::is_trivially_destructible #include #include using namespace std; struct Y { Y(int, int){}; }; struct X { // Destructor ~X() noexcept(false) { } }; struct Z { ~Z() = default; }; class A { virtual void fn() {} }; class MyBvClass { sc_dt::sc_bv<2> m_test_mem; }; int sc_main(int argc, char **argv) { cout << boolalpha; cout << "int:" << std::is_trivially_destructible::value << endl; cout << "struct X:" << std::is_trivially_destructible::value << endl; cout << "struct Y:" << std::is_trivially_destructible::value << endl; cout << "struct Z:" << std::is_trivially_destructible::value << endl; cout << "class A:" << std::is_trivially_destructible::value << endl; cout << "class MyBvClass:" << std::is_trivially_destructible::value << endl; sc_core::sc_start(); return 0; }

【C/C++|std::is_trivially_destructible】

    推荐阅读