C++ 标准在 https://eel.is/c++draft/utility.arg.requirements#:Cpp17Destructible:~:text=%5Btab%3Acpp17.destructible%5D 定义了 Cpp17Destructible
我可以使用一个特征来在代码中检查这一点吗?
std::is_destructible
似乎没有做正确的事情,因为以下代码编译:
#include <type_traits>
static_assert(std::is_destructible_v<int>);
using f_ptr = int(*)(int);
static_assert(std::is_destructible_v<f_ptr>);
static_assert(std::is_destructible_v<int[4]>);
std::is_destructible
在语法要求方面几乎相同,只是它为数组和引用类型提供了true
,所以
template<typename T>
struct is_cpp17destructible
: std::bool_constant<std::is_destructible_v<T> &&
!std::is_array_v<T> && !std::is_reference_v<T>> { };
将作为特质。
但是,Cpp17Destructible还有一个语义要求,即析构函数不会抛出任何异常。您无法使用类型特征来测试这一点。这与要求析构函数是
noexcept
不同,可以用std::is_nothrow_destructible
而不是std::is_destructible
来检查。