std::is_trivially_destructible 判断一个类型是否是一个平凡的可销毁类型
C++ 11知识点:std::is_trivially_destructible
简介
判断一个类型T是否是一个平凡的可销毁类型(trivivally destructible)。主要用于检查这个类型的析构函数。
一个trivivally destructible类(由class,struct/union)这样定义:
- uses the implicitly defined destructor.
- the destructor is not virtual.
- its base class and non-static data members (if any) are themselves also trivially destructible types.
class template声明
#include <type_traits>
template <class T> struct is_trivially_destructible;
is_trivially_destructible 内部继承integral_constant作为true_type或false_type,依赖于T是否是trivivally destructible。
示例
// is_trivially_destructible example
#include <iostream>
#include <type_traits>
struct A { }; /* 符合trivivally destructible类型定义 */
struct B { ~B(){} }; /* 没有使用隐式应答的析构函数, 即编译器合成的默认析构函数, 因此不是trivivally destructible类型 */
int main() {
std::cout << std::boolalpha; /* 将输出流bool解析为true/false, 而不是1/0 */
std::cout << "is_trivially_destructible:" << std::endl;
std::cout << "int: " << std::is_trivially_destructible<int>::value << std::endl; /* 基本类型是trivivally destructible类型 */
std::cout << "A: " << std::is_trivially_destructible<A>::value << std::endl; /* A是trivivally destructible类型 */
std::cout << "B: " << std::is_trivially_destructible<B>::value << std::endl; /* B不是trivivally destructible类型 */
return 0;
}
输出:
is_trivially_destructible:
int: true
A: true
B: false
类似的类模板还有
- is_constructible 判断一个类型是不是可以用指定参数集构造的类型
- is_trivial 判断一个类型是否是一个平凡的类型
- is_trivially_constructible 判断一个类型是否是可以用指定参数集构造的平凡的可构造类型
- is_nothrow_destructible 判断一个类型是否是可析构类型,并且不抛出任何异常
参考
http://www.cplusplus.com/reference/type_traits/is_trivially_destructible/
本文作者:明明1109
本文链接:https://www.cnblogs.com/fortunely/p/16216303.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步