std:: is_destructible, std:: is_trivially_destructible, std:: is_nothrow_destructible
From cppreference.net
|
定义于头文件
<type_traits>
|
||
|
template
<
class
T
>
struct is_destructible ; |
(1) | (C++11 起) |
|
template
<
class
T
>
struct is_trivially_destructible ; |
(2) | (C++11 起) |
|
template
<
class
T
>
struct is_nothrow_destructible ; |
(3) | (C++11 起) |
1)
若
T
为引用类型,则提供等于
true
的成员常量
value
。
若
T
是(可能带有 cv 限定符的)
void
、函数类型或未知边界数组,则
value
等于
false
。
若
T
是对象类型,则对于类型
U
(即
std::
remove_all_extents
<
T
>
::
type
),若表达式
std::
declval
<
U
&
>
(
)
.~U
(
)
在未求值语境中合法,则
value
等于
true
。否则
value
等于
false
。
3)
与
(1)
相同,但析构函数为
noexcept
。
如果
T
不是完整类型、(可能带有 cv 限定符的)
void
,或未知边界的数组,则行为未定义。
如果上述模板的实例化直接或间接依赖于不完整类型,且该实例化在该类型被假设完成时可能产生不同结果,则行为未定义。
如果程序对本页面描述的任何模板添加特化,则行为未定义。
目录 |
辅助变量模板
|
template
<
class
T
>
constexpr bool is_destructible_v = is_destructible < T > :: value ; |
(自 C++17 起) | |
|
template
<
class
T
>
constexpr bool is_trivially_destructible_v = is_trivially_destructible < T > :: value ; |
(自 C++17 起) | |
|
template
<
class
T
>
constexpr bool is_nothrow_destructible_v = is_nothrow_destructible < T > :: value ; |
(自 C++17 起) | |
继承自 std:: integral_constant
成员常量
|
value
[static]
|
若
T
可析构则为
true
,否则为
false
(公开静态成员常量) |
成员函数
|
operator bool
|
转换对象为
bool
,返回
value
(公开成员函数) |
|
operator()
(C++14)
|
返回
value
(公开成员函数) |
成员类型
| 类型 | 定义 |
value_type
|
bool |
type
|
std:: integral_constant < bool , value > |
注释
由于C++程序在栈回溯期间若析构函数抛出异常(这通常无法预测)会导致程序终止,因此所有实际可用的析构函数即使未声明为noexcept也都是不抛异常的。C++标准库中的所有析构函数均被设计为不抛异常。
由 可平凡析构 对象占用的存储空间 可被重用 而无需调用析构函数。
可能的实现
| is_destructible (1) |
|---|
// C++20 required template<typename t> struct is_destructible : std::integral_constant<bool, requires(t object) { object.~t(); }> {}; |
| is_trivially_destructible (2) |
// Not real C++. Shall P2996 be approved, the following implementation will be available: template<typename t> struct is_trivially_destructible : std::integral_constant<bool, std::meta::type_is_trivially_destructible(^t)> {}; |
| is_nothrow_destructible (3) |
// C++20 required template<typename t> struct is_nothrow_destructible : std::integral_constant<bool, requires(t object) { {object.~t()} noexcept; }> {}; |
示例
运行此代码
#include <iostream> #include <string> #include <type_traits> struct Foo { std::string str; ~Foo() noexcept {}; }; struct Bar { ~Bar() = default; }; static_assert(std::is_destructible<std::string>::value == true); static_assert(std::is_trivially_destructible_v<Foo> == false); static_assert(std::is_nothrow_destructible<Foo>() == true); static_assert(std::is_trivially_destructible<Bar>{} == true); int main() {}
缺陷报告
以下行为变更缺陷报告被追溯应用于先前发布的C++标准。
| 缺陷报告 | 应用于 | 发布时的行为 | 正确行为 |
|---|---|---|---|
| LWG 2049 | C++11 | 由于虚构的包装结构体导致规范无法完成 | 已完善 |
参见
|
(C++11)
(C++11)
(C++11)
|
检查类型是否具有特定参数的构造函数
(类模板) |
|
(C++11)
|
检查类型是否具有虚析构函数
(类模板) |
|
(C++20)
|
指定该类型的对象可以被销毁
(概念) |
| destructor | 释放已申请的资源 |