C++标准模板(STL)

时间: 2023-11-14 admin 维修知识

C++标准模板(STL)

C++标准模板(STL)

类型特性


类型特性定义一个编译时基于模板的结构,以查询或修改类型的属性。

试图特化定义于 <type_traits> 头文件的模板导致未定义行为,除了 std::common_type 可依照其所描述特化。

定义于<type_traits>头文件的模板可以用不完整类型实例化,除非另外有指定,尽管通常禁止以不完整类型实例化标准库模板。
 

受支持操作

继承自 std::integral_constant

成员常量

value

[静态]

T 可析构则为 true ,否则为 false
(公开静态成员常量)

成员函数

operator bool

转换对象为 bool ,返回 value
(公开成员函数)

operator()

(C++14)

返回 value
(公开成员函数)

成员类型

类型定义
value_typebool
typestd::integral_constant<bool, value>

检查类型是否拥有未被弃置的析构函数

std::is_destructible, 
std::is_trivially_destructible, 
std::is_nothrow_destructible

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) 若 T 是引用类型,则提供等于 true 的成员常量 value

T 是不完整类型(包含 void )或函数类型,则 value 等于 false 。

T 是对象类型,则对于等于 std::remove_all_extents<T>::type 的类型 U ,若表达式 std::declval<U&>().~U() 在不求值语境合法,则 value 等于 true 。否则, value 等于 false 。

(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 起)

注意

因为若析构函数在栈回溯中抛出异常则 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::cout << "int is destructible? "<< std::is_destructible<int>::value << std::endl;std::cout << "int is_trivially_destructible? "<< std::is_trivially_destructible<int>::value << std::endl;std::cout << "int is_nothrow_destructible? "<< std::is_nothrow_destructible<int>::value << std::endl;std::cout << "std::string is destructible? "<< std::is_destructible<std::string>::value << std::endl;std::cout << "std::string is_trivially_destructible? "<< std::is_trivially_destructible<std::string>::value << std::endl;std::cout << "std::string is_nothrow_destructible? "<< std::is_nothrow_destructible<std::string>::value << std::endl;std::cout << "Foo is destructible? "<< std::is_destructible<Foo>::value << std::endl;std::cout << "Foo is_trivially_destructible? "<< std::is_trivially_destructible<Foo>::value << std::endl;std::cout << "Foo is nothrow destructible? "<< std::is_nothrow_destructible<Foo>::value << std::endl;std::cout << "Bar is destructible? "<< std::is_destructible<Bar>::value << std::endl;std::cout << "Bar is trivally destructible? "<< std::is_trivially_destructible<Bar>::value << std::endl;std::cout << "Bar is nothrow destructible? "<< std::is_nothrow_destructible<Bar>::value << std::endl;return 0;
}

输出