定义于头文件
<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 。对于任何其他类型, value 为 false 。 |
(C++14 前) |
1) 若 若 若 |
(C++14 起) |
2) 同 1) ,并且 std::remove_all_extents<T>::type 要么是非类类型,要么是拥有平凡析构函数的类类型。
3) 同 1) ,但析构函数为 noexcept 。
T
应是完整类型、(可为 cv 限定的) void ,或未知边界数组。否则行为未定义。
目录 |
template< class T >
inline constexpr bool is_destructible_v = is_destructible<T>::value; |
(C++17 起) | |
template< class T >
inline constexpr bool is_trivially_destructible_v = is_trivially_destructible<T>::value; |
(C++17 起) | |
template< class T >
inline constexpr bool is_nothrow_destructible_v = is_nothrow_destructible<T>::value; |
(C++17 起) | |
value
[静态]
|
若 T 可析构则为 true ,否则为 false (公开静态成员常量) |
operator bool
|
转换对象为 bool ,返回 value (公开成员函数) |
operator()
(C++14)
|
返回 value (公开成员函数) |
类型 | 定义 |
value_type
|
bool
|
type
|
std::integral_constant<bool, value> |
因为若析构函数在栈回溯中抛出异常则 C++ 程序终止(通常是无法预期的),所有实用的析构函数均为不抛出,即使它们不声明为 noexcept 。所有 C++ 标准库中能找到的析构函数都是不抛出的。
可平凡析构对象所占用的存储可以重用而无需调用析构函数。
#include <iostream> #include <string> #include <type_traits> struct Foo { std::string str; ~Foo() noexcept {}; }; struct Bar { ~Bar() = default; }; int main() { std::cout << std::boolalpha << "std::string is destructible? " << std::is_destructible<std::string>::value << '\n' << "Foo is nothrow destructible? " << std::is_nothrow_destructible<Foo>::value << '\n' << "Bar is trivally destructible? " << std::is_trivially_destructible<Bar>::value << '\n'; }
输出:
std::string is destructible? true Foo is nothrow destructible? true Bar is trivally destructible? true
(C++11)
(C++11) (C++11) |
检查对象是否实现了特定参数的构造函数 (类模板) |
(C++11)
|
检查类型是否拥有虚析构函数 (类模板) |