标准库头文件 <memory>
来自cppreference.com
此头文件是动态内存管理库的一部分。
类 | |
指针特征 | |
(C++11) |
提供关于指针式类型的信息 (类模板) |
垃圾收集器支持 | |
(C++11) |
列出指针安全模式 (枚举) |
分配器 | |
默认的分配器 (类模板) | |
(C++11) |
提供关于分配器类型的信息 (类模板) |
(C++11) |
标签类型,用于选择具分配器的构造函数重载 (类) |
(C++11) |
用于选择具分配器的构造函数的 std::allocator_arg_t 对象 (常量) |
(C++11) |
检查指定的类型是否支持使用分配器的构造 (类模板) |
未初始化存储 | |
(C++17 中弃用)(C++20 中移除) |
允许标准算法存储结果于未初始化内存的迭代器 (类模板) |
智能指针 | |
(C++11) |
拥有独有对象所有权语义的智能指针 (类模板) |
(C++11) |
拥有共享对象所有权语义的智能指针 (类模板) |
(C++11) |
到 std::shared_ptr 所管理对象的弱引用 (类模板) |
(C++17 中移除) |
拥有严格对象所有权语义的智能指针 (类模板) |
辅助类 | |
(C++20) |
原子共享指针 (类模板特化) |
(C++20) |
原子弱指针 (类模板特化) |
(C++11) |
提供基于所有者的,共享指针和弱指针的混合类型的排序 (类模板) |
(C++11) |
允许对象创建指代自身的 shared_ptr (类模板) |
(C++11) |
访问指向已销毁对象的 weak_ptr 时抛出的异常 (类) |
(C++11) |
unique_ptr 的默认删除器 (类模板) |
(C++11) |
std::unique_ptr 的散列支持 (类模板特化) |
(C++11) |
std::shared_ptr 的散列支持 (类模板特化) |
函数 | |
杂项 | |
(C++20) |
从指针式类型获得裸指针 (函数模板) |
(C++11) |
获得对象的实际地址,即使其重载了 & 运算符 (函数模板) |
(C++11) |
对齐缓冲区中的指针 (函数) |
(C++20) |
告知编译器指针已对齐 (函数模板) |
垃圾收集器支持 | |
(C++11) |
声明一个对象不能被回收 (函数) |
(C++11) |
声明一个对象可以被回收 (函数模板) |
(C++11) |
声明该内存区域不含可追踪指针 (函数) |
(C++11) |
取消 std::declare_no_pointers 的效果 (函数) |
(C++11) |
返回当前的指针安全模式 (函数) |
未初始化存储 | |
将范围内的对象复制到未初始化的内存区域 (函数模板) | |
(C++11) |
将指定数量的对象复制到未初始化的内存区域 (函数模板) |
复制一个对象到以范围定义的未初始化内存区域 (函数模板) | |
复制一个对象到以起点和计数定义的未初始化内存区域 (函数模板) | |
(C++17) |
移动一个范围的对象到未初始化的内存区域 (函数模板) |
(C++17) |
移动一定数量对象到未初始化内存区域 (函数模板) |
在范围所定义的未初始化的内存区域以默认初始化构造对象 (函数模板) | |
在起始和计数所定义的未初始化内存区域用默认初始化构造对象 (函数模板) | |
在范围所定义的未初始化内存中用值初始化构造对象 (函数模板) | |
在起始和计数所定义的未初始化内存区域以值初始化构造对象 (函数模板) | |
(C++17) |
销毁在给定地址的对象 (函数模板) |
(C++17) |
销毁一个范围中的对象 (函数模板) |
(C++17) |
销毁范围中一定数量的对象 (函数模板) |
(C++17 中弃用)(C++20 中移除) |
获得未初始化存储 (函数模板) |
(C++17 中弃用)(C++20 中移除) |
释放未初始化存储 (函数模板) |
智能指针非成员操作 | |
(C++14)(C++20) |
创建管理一个新对象的独占指针 (函数模板) |
与另一个 unique_ptr 或 nullptr 进行比较 (函数模板) | |
创建管理一个新对象的共享指针 (函数模板) | |
创建管理一个用分配器分配的新对象的共享指针 (函数模板) | |
应用 static_cast、dynamic_cast、const_cast 或 reinterpret_cast 到被存储指针 (函数模板) | |
返回指定类型中的删除器,若其拥有 (函数模板) | |
与另一个 shared_ptr 或 nullptr 进行比较 (函数模板) | |
将存储的指针的值输出到输出流 (函数模板) | |
(C++11) |
特化 std::swap 算法 (函数模板) |
(C++11) |
特化 std::swap 算法 (函数模板) |
(C++11) |
特化 std::swap 算法 (函数模板) |
特化的原子操作 (函数模板) |
Niebloid | |
定义于命名空间
std::ranges | |
未初始化存储 | |
(C++20) |
复制元素范围到未初始化的内存区域 (niebloid) |
(C++20) |
复制一定量元素到未初始化的内存区域 (niebloid) |
(C++20) |
复制一个对象到范围所定义的未初始化的内存区域 (niebloid) |
(C++20) |
复制一个对象到起始与计数所定义的未初始化的内存区域 (niebloid) |
(C++20) |
移动对象范围到未初始化的内存区域 (niebloid) |
(C++20) |
移动一定量对象到未初始化的内存区域 (niebloid) |
在范围所定义的未初始化的内存区域以默认初始化构造对象 (niebloid) | |
在起始与计数所定义的未初始化的内存区域以默认初始化构造对象 (niebloid) | |
在范围所定义的未初始化的内存区域以值初始化构造对象 (niebloid) | |
在起始与计数所定义的未初始化的内存区域以值初始化构造对象 (niebloid) | |
(C++20) |
销毁位于给定地址的元素 (niebloid) |
(C++20) |
销毁范围中的元素 (niebloid) |
(C++20) |
销毁范围中一定量的元素 (niebloid) |
概要
namespace std { // 指针特征 template<class Ptr> struct pointer_traits; template<class T> struct pointer_traits<T*>; // 指针转换 template<class T> constexpr T* to_address(T* p) noexcept; template<class Ptr> auto to_address(const Ptr& p) noexcept; // 指针安全 enum class pointer_safety { relaxed, preferred, strict }; void declare_reachable(void* p); template<class T> T* undeclare_reachable(T* p); void declare_no_pointers(char* p, size_t n); void undeclare_no_pointers(char* p, size_t n); pointer_safety get_pointer_safety() noexcept; // 指针对齐 void* align(size_t alignment, size_t size, void*& ptr, size_t& space); template<size_t N, class T> [[nodiscard]] constexpr T* assume_aligned(T* ptr); // 分配器实参标签 struct allocator_arg_t { explicit allocator_arg_t() = default; }; inline constexpr allocator_arg_t allocator_arg{}; // uses_allocator template<class T, class Alloc> struct uses_allocator; // uses_allocator template<class T, class Alloc> inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value; // 使用分配器构造 template<class T, class Alloc, class... Args> constexpr auto uses_allocator_construction_args(const Alloc& alloc, Args&&... args) noexcept -> /* 见定义 */; template<class T, class Alloc, class Tuple1, class Tuple2> constexpr auto uses_allocator_construction_args( const Alloc& alloc, piecewise_construct_t, Tuple1&& x, Tuple2&& y) noexcept -> /* 见定义 */; template<class T, class Alloc> constexpr auto uses_allocator_construction_args(const Alloc& alloc) noexcept -> /* 见定义 */; template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, U&& u, V&& v) noexcept -> /* 见定义 */; template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, const pair<U,V>& pr) noexcept -> /* 见定义 */; template<class T, class Alloc, class U, class V> constexpr auto uses_allocator_construction_args(const Alloc& alloc, pair<U,V>&& pr) noexcept -> /* 见定义 */; template<class T, class Alloc, class... Args> constexpr T make_obj_using_allocator(const Alloc& alloc, Args&&... args); // 分配器特征 template<class Alloc> struct allocator_traits; // 默认分配器 template<class T> class allocator; template<class T, class U> bool operator==(const allocator<T>&, const allocator<U>&) noexcept; template<class T, class U> bool operator!=(const allocator<T>&, const allocator<U>&) noexcept; // 特化的算法 // 特殊内存概念 template<class I> concept __NoThrowInputIterator = /* 见定义 */; // 仅用于阐释 template<class I> concept __NoThrowForwardIterator = /* 见定义 */; // 仅用于阐释 template<class S, class I> concept __NoThrowSentinel = /* 见定义 */; // 仅用于阐释 template<class R> concept __NoThrowInputRange = /* 见定义 */; // 仅用于阐释 template<class R> concept __NoThrowForwardRange = /* 见定义 */; // 仅用于阐释 template<class T> constexpr T* addressof(T& r) noexcept; template<class T> const T* addressof(const T&&) = delete; template<class ForwardIt> void uninitialized_default_construct(ForwardIt first, ForwardIt last); template<class ExecutionPolicy, class ForwardIt> void uninitialized_default_construct(ExecutionPolicy&& exec, ForwardIt first, ForwardIt last); template<class ForwardIt, class Size> ForwardIt uninitialized_default_construct_n(ForwardIt first, Size n); template<class ExecutionPolicy, class ForwardIt, class Size> ForwardIt uninitialized_default_construct_n(ExecutionPolicy&& exec, ForwardIt first, Size n); namespace ranges { template<__NoThrowForwardIterator I, __NoThrowSentinel<I> S> requires DefaultConstructible<iter_value_t<I>> I uninitialized_default_construct(I first, S last); template<__NoThrowForwardRange R> requires DefaultConstructible<iter_value_t<iterator_t<R>>> safe_iterator_t<R> uninitialized_default_construct(R&& r); template<__NoThrowForwardIterator I> requires DefaultConstructible<iter_value_t<I>> I uninitialized_default_construct_n(I first, iter_difference_t<I> n); } template<class ForwardIt> void uninitialized_value_construct(ForwardIt first, ForwardIt last); template<class ExecutionPolicy, class ForwardIt> void uninitialized_value_construct(ExecutionPolicy&& exec, ForwardIt first, ForwardIt last); template<class ForwardIt, class Size> ForwardIt uninitialized_value_construct_n(ForwardIt first, Size n); template<class ExecutionPolicy, class ForwardIt, class Size> ForwardIt uninitialized_value_construct_n(ExecutionPolicy&& exec, ForwardIt first, Size n); namespace ranges { template<__NoThrowForwardIterator I, __NoThrowSentinel<I> S> requires DefaultConstructible<iter_value_t<I>> I uninitialized_value_construct(I first, S last); template<__NoThrowForwardRange R> requires DefaultConstructible<iter_value_t<iterator_t<R>>> safe_iterator_t<R> uninitialized_value_construct(R&& r); template<__NoThrowForwardIterator I> requires DefaultConstructible<iter_value_t<I>> I uninitialized_value_construct_n(I first, iter_difference_t<I> n); } template<class InputIt, class ForwardIt> ForwardIt uninitialized_copy(InputIt first, InputIt last, ForwardIt result); template<class ExecutionPolicy, class InputIt, class ForwardIt> ForwardIt uninitialized_copy(ExecutionPolicy&& exec, InputIt first, InputIt last, ForwardIt result); template<class InputIt, class Size, class ForwardIt> ForwardIt uninitialized_copy_n(InputIt first, Size n, ForwardIt result); template<class ExecutionPolicy, class InputIt, class Size, class ForwardIt> ForwardIt uninitialized_copy_n(ExecutionPolicy&& exec, InputIt first, Size n, ForwardIt result); namespace ranges { template<class I, class O> using uninitialized_copy_result = copy_result<I, O>; template<InputIterator I, Sentinel<I> S1, __NoThrowForwardIterator O, __NoThrowSentinel<O> S2> requires Constructible<iter_value_t<O>, iter_reference_t<I>> uninitialized_copy_result<I, O> uninitialized_copy(I ifirst, S1 ilast, O ofirst, S2 olast); template<InputRange IR, __NoThrowForwardRange OR> requires Constructible<iter_value_t<iterator_t<OR>>, iter_reference_t<iterator_t<IR>>> uninitialized_copy_result<safe_iterator_t<IR>, safe_iterator_t<OR>> uninitialized_copy(IR&& input_range, OR&& output_range); template<class I, class O> using uninitialized_copy_n_result = uninitialized_copy_result<I, O>; template<InputIterator I, __NoThrowForwardIterator O, __NoThrowSentinel<O> S> requires Constructible<iter_value_t<O>, iter_reference_t<I>> uninitialized_copy_n_result<I, O> uninitialized_copy_n(I ifirst, iter_difference_t<I> n, O ofirst, S olast); } template<class InputIt, class ForwardIt> ForwardIt uninitialized_move(InputIt first, InputIt last, ForwardIt result); template<class ExecutionPolicy, class InputIt, class ForwardIt> ForwardIt uninitialized_move(ExecutionPolicy&& exec, InputIt first, InputIt last, ForwardIt result); template<class InputIt, class Size, class ForwardIt> pair<InputIt, ForwardIt> uninitialized_move_n(InputIt first, Size n, ForwardIt result); template<class ExecutionPolicy, class InputIt, class Size, class ForwardIt> pair<InputIt, ForwardIt> uninitialized_move_n(ExecutionPolicy&& exec, InputIt first, Size n, ForwardIt result); namespace ranges { template<class I, class O> using uninitialized_move_result = uninitialized_copy_result<I, O>; template<InputIterator I, Sentinel<I> S1, __NoThrowForwardIterator O, __NoThrowSentinel<O> S2> requires Constructible<iter_value_t<O>, iter_rvalue_reference_t<I>> uninitialized_move_result<I, O> uninitialized_move(I ifirst, S1 ilast, O ofirst, S2 olast); template<InputRange IR, __NoThrowForwardRange OR> requires Constructible<iter_value_t<iterator_t<OR>>, iter_rvalue_reference_t<iterator_t<IR>>> uninitialized_move_result<safe_iterator_t<IR>, safe_iterator_t<OR>> uninitialized_move(IR&& input_range, OR&& output_range); template<class I, class O> using uninitialized_move_n_result = uninitialized_copy_result<I, O>; template<InputIterator I, __NoThrowForwardIterator O, __NoThrowSentinel<O> S> requires Constructible<iter_value_t<O>, iter_rvalue_reference_t<I>> uninitialized_move_n_result<I, O> uninitialized_move_n(I ifirst, iter_difference_t<I> n, O ofirst, S olast); } template<class ForwardIt, class T> void uninitialized_fill(ForwardIt first, ForwardIt last, const T& x); template<class ExecutionPolicy, class ForwardIt, class T> void uninitialized_fill(ExecutionPolicy&& exec, ForwardIt first, ForwardIt last, const T& x); template<class ForwardIt, class Size, class T> ForwardIt uninitialized_fill_n(ForwardIt first, Size n, const T& x); template<class ExecutionPolicy, class ForwardIt, class Size, class T> ForwardIt uninitialized_fill_n(ExecutionPolicy&& exec, ForwardIt first, Size n, const T& x); namespace ranges { template<__NoThrowForwardIterator I, __NoThrowSentinel<I> S, class T> requires Constructible<iter_value_t<I>, const T&> I uninitialized_fill(I first, S last, const T& x); template<__NoThrowForwardRange R, class T> requires Constructible<iter_value_t<iterator_t<R>>, const T&> safe_iterator_t<R> uninitialized_fill(R&& r, const T& x); template<__NoThrowForwardIterator I, class T> requires Constructible<iter_value_t<I>, const T&> I uninitialized_fill_n(I first, iter_difference_t<I> n, const T& x); } template<class T> void destroy_at(T* location); template<class ForwardIt> void destroy(ForwardIt first, ForwardIt last); template<class ExecutionPolicy, class ForwardIt> void destroy(ExecutionPolicy&& exec, ForwardIt first, ForwardIt last); template<class ForwardIt, class Size> ForwardIt destroy_n(ForwardIt first, Size n); template<class ExecutionPolicy, class ForwardIt, class Size> ForwardIt destroy_n(ExecutionPolicy&& exec, ForwardIt first, Size n); namespace ranges { template<Destructible T> void destroy_at(T* location) noexcept; template<__NoThrowInputIterator I, __NoThrowSentinel<I> S> requires Destructible<iter_value_t<I>> I destroy(I first, S last) noexcept; template<__NoThrowInputRange R> requires Destructible<iter_value_t<iterator_t<R>> safe_iterator_t<R> destroy(R&& r) noexcept; template<__NoThrowInputIterator I> requires Destructible<iter_value_t<I>> I destroy_n(I first, iter_difference_t<I> n) noexcept; } // 类模板 unique_ptr template<class T> struct default_delete; template<class T> struct default_delete<T[]>; template<class T, class D = default_delete<T>> class unique_ptr; template<class T, class D> class unique_ptr<T[], D>; template<class T, class... Args> unique_ptr<T> make_unique(Args&&... args); // T 非数组 template<class T> unique_ptr<T> make_unique(size_t n); // T 为 U[] template<class T, class... Args> /* 未指明 */ make_unique(Args&&...) = delete; // T 为 U[N] template<class T> unique_ptr<T> make_unique_default_init(); // T 非数组 template<class T> unique_ptr<T> make_unique_default_init(size_t n); // T 为 U[] template<class T, class... Args> /* 未指明 */ make_unique_default_init(Args&&...) = delete; // T 为 U[N] template<class T, class D> void swap(unique_ptr<T, D>& x, unique_ptr<T, D>& y) noexcept; template<class T1, class D1, class T2, class D2> bool operator==(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2> bool operator!=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2> bool operator<(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2> bool operator>(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2> bool operator<=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); template<class T1, class D1, class T2, class D2> bool operator>=(const unique_ptr<T1, D1>& x, const unique_ptr<T2, D2>& y); template<class T, class D> bool operator==(const unique_ptr<T, D>& x, nullptr_t) noexcept; template<class T, class D> bool operator==(nullptr_t, const unique_ptr<T, D>& y) noexcept; template<class T, class D> bool operator!=(const unique_ptr<T, D>& x, nullptr_t) noexcept; template<class T, class D> bool operator!=(nullptr_t, const unique_ptr<T, D>& y) noexcept; template<class T, class D> bool operator<(const unique_ptr<T, D>& x, nullptr_t); template<class T, class D> bool operator<(nullptr_t, const unique_ptr<T, D>& y); template<class T, class D> bool operator>(const unique_ptr<T, D>& x, nullptr_t); template<class T, class D> bool operator>(nullptr_t, const unique_ptr<T, D>& y); template<class T, class D> bool operator<=(const unique_ptr<T, D>& x, nullptr_t); template<class T, class D> bool operator<=(nullptr_t, const unique_ptr<T, D>& y); template<class T, class D> bool operator>=(const unique_ptr<T, D>& x, nullptr_t); template<class T, class D> bool operator>=(nullptr_t, const unique_ptr<T, D>& y); template<class E, class T, class Y, class D> basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const unique_ptr<Y, D>& p); // 类 bad_weak_ptr class bad_weak_ptr; // 类模板 shared_ptr template<class T> class shared_ptr; // shared_ptr 创建 template<class T, class... Args> shared_ptr<T> make_shared(Args&&... args); // T 非数组 template<class T, class A, class... Args> shared_ptr<T> allocate_shared(const A& a, Args&&... args); // T 非数组 template<class T> shared_ptr<T> make_shared(size_t N); // T 为 U[] template<class T, class A> shared_ptr<T> allocate_shared(const A& a, size_t N); // T 为 U[] template<class T> shared_ptr<T> make_shared(); // T 为 U[N] template<class T, class A> shared_ptr<T> allocate_shared(const A& a); // T 为 U[N] template<class T> shared_ptr<T> make_shared(size_t N, const remove_extent_t<T>& u); // T 为 U[] template<class T, class A> shared_ptr<T> allocate_shared(const A& a, size_t N, const remove_extent_t<T>& u); // T 为 U[] template<class T> shared_ptr<T> make_shared(const remove_extent_t<T>& u); // T 为 U[N] template<class T, class A> shared_ptr<T> allocate_shared(const A& a, const remove_extent_t<T>& u); // T 为 U[N] template<class T> shared_ptr<T> make_shared_default_init(); // T 非 U[] template<class T, class A> shared_ptr<T> allocate_shared_default_init(const A& a); // T 非 U[] template<class T> shared_ptr<T> make_shared_default_init(size_t N); // T 为 U[] template<class T, class A> shared_ptr<T> allocate_shared_default_init(const A& a, size_t N); // T 为 U[] // shared_ptr 比较 template<class T, class U> bool operator==(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept; template<class T, class U> bool operator!=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept; template<class T, class U> bool operator<(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept; template<class T, class U> bool operator>(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept; template<class T, class U> bool operator<=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept; template<class T, class U> bool operator>=(const shared_ptr<T>& a, const shared_ptr<U>& b) noexcept; template<class T> bool operator==(const shared_ptr<T>& x, nullptr_t) noexcept; template<class T> bool operator==(nullptr_t, const shared_ptr<T>& y) noexcept; template<class T> bool operator!=(const shared_ptr<T>& x, nullptr_t) noexcept; template<class T> bool operator!=(nullptr_t, const shared_ptr<T>& y) noexcept; template<class T> bool operator<(const shared_ptr<T>& x, nullptr_t) noexcept; template<class T> bool operator<(nullptr_t, const shared_ptr<T>& y) noexcept; template<class T> bool operator>(const shared_ptr<T>& x, nullptr_t) noexcept; template<class T> bool operator>(nullptr_t, const shared_ptr<T>& y) noexcept; template<class T> bool operator<=(const shared_ptr<T>& x, nullptr_t) noexcept; template<class T> bool operator<=(nullptr_t, const shared_ptr<T>& y) noexcept; template<class T> bool operator>=(const shared_ptr<T>& x, nullptr_t) noexcept; template<class T> bool operator>=(nullptr_t, const shared_ptr<T>& y) noexcept; // shared_ptr 特化算法 template<class T> void swap(shared_ptr<T>& a, shared_ptr<T>& b) noexcept; // shared_ptr 转型 template<class T, class U> shared_ptr<T> static_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> shared_ptr<T> static_pointer_cast(shared_ptr<U>&& r) noexcept; template<class T, class U> shared_ptr<T> dynamic_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> shared_ptr<T> dynamic_pointer_cast(shared_ptr<U>&& r) noexcept; template<class T, class U> shared_ptr<T> const_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> shared_ptr<T> const_pointer_cast(shared_ptr<U>&& r) noexcept; template<class T, class U> shared_ptr<T> reinterpret_pointer_cast(const shared_ptr<U>& r) noexcept; template<class T, class U> shared_ptr<T> reinterpret_pointer_cast(shared_ptr<U>&& r) noexcept; // shared_ptr get_deleter template<class D, class T> D* get_deleter(const shared_ptr<T>& p) noexcept; // shared_ptr I/O template<class E, class T, class Y> basic_ostream<E, T>& operator<<(basic_ostream<E, T>& os, const shared_ptr<Y>& p); // 类模板 weak_ptr template<class T> class weak_ptr; // weak_ptr 特化算法 template<class T> void swap(weak_ptr<T>& a, weak_ptr<T>& b) noexcept; // 类模板 owner_less template<class T = void> struct owner_less; // 类模板 enable_shared_from_this template<class T> class enable_shared_from_this; // 散列支持 template<class T> struct hash; template<class T, class D> struct hash<unique_ptr<T, D>>; template<class T> struct hash<shared_ptr<T>>; // 原子智能指针 template<class T> struct atomic; template<class T> struct atomic<shared_ptr<T>>; template<class T> struct atomic<weak_ptr<T>>; // shared_ptr 原子访问 template<class T> bool atomic_is_lock_free(const shared_ptr<T>* p); template<class T> shared_ptr<T> atomic_load(const shared_ptr<T>* p); template<class T> shared_ptr<T> atomic_load_explicit(const shared_ptr<T>* p, memory_order mo); template<class T> void atomic_store(shared_ptr<T>* p, shared_ptr<T> r); template<class T> void atomic_store_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); template<class T> shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r); template<class T> shared_ptr<T> atomic_exchange_explicit(shared_ptr<T>* p, shared_ptr<T> r, memory_order mo); template<class T> bool atomic_compare_exchange_weak(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); template<class T> bool atomic_compare_exchange_strong(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w); template<class T> bool atomic_compare_exchange_weak_explicit(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w, memory_order success, memory_order failure); template<class T> bool atomic_compare_exchange_strong_explicit(shared_ptr<T>* p, shared_ptr<T>* v, shared_ptr<T> w, memory_order success, memory_order failure); }
辅助概念
template<class I> concept __NoThrowInputIterator = // 仅用于阐释 InputIterator<I> && is_lvalue_reference_v<iter_reference_t<I>> && Same<remove_cvref_t<iter_reference_t<I>>, iter_value_t<I>>; template<class S, class I> concept __NoThrowSentinel = Sentinel<S, I>; // 仅用于阐释 template<class R> concept __NoThrowInputRange = // 仅用于阐释 Range<R> && __NoThrowInputIterator<iterator_t<R>> && __NoThrowSentinel<sentinel_t<R>, iterator_t<R>>; template<class I> concept __NoThrowForwardIterator = // 仅用于阐释 __NoThrowInputIterator<I> && ForwardIterator<I> && __NoThrowSentinel<I, I>; template<class R> concept __NoThrowForwardRange = // 仅用于阐释 __NoThrowInputRange<R> && __NoThrowForwardIterator<iterator_t<R>>;
注意:这些名称仅用于阐释目的,它们并不是接口的一部分。
类模板 std::pointer_traits
namespace std { template <class Ptr> struct pointer_traits { using pointer = Ptr; using element_type = /* 见定义 */; using difference_type = /* 见定义 */; template <class U> using rebind = /* 见定义 */; static pointer pointer_to(/* 见定义 */ r); }; template <class T> struct pointer_traits<T*> { using pointer = T*; using element_type = T; using difference_type = ptrdiff_t; template <class U> using rebind = U*; static constexpr pointer pointer_to(/* 见定义 */ r) noexcept; }; }
类 std::allocator_arg_t
namespace std { struct allocator_arg_t { explicit allocator_arg_t() = default; }; inline constexpr allocator_arg_t allocator_arg{}; }
类模板 std::allocator_traits
namespace std { template<class Alloc> struct allocator_traits { using allocator_type = Alloc; using value_type = typename Alloc::value_type; using pointer = /* 见定义 */; using const_pointer = /* 见定义 */; using void_pointer = /* 见定义 */; using const_void_pointer = /* 见定义 */; using difference_type = /* 见定义 */; using size_type = /* 见定义 */; using propagate_on_container_copy_assignment = /* 见定义 */; using propagate_on_container_move_assignment = /* 见定义 */; using propagate_on_container_swap = /* 见定义 */; using is_always_equal = /* 见定义 */; template<class T> using rebind_alloc = /* 见定义 */; template<class T> using rebind_traits = allocator_traits<rebind_alloc<T>>; [[nodiscard]] static pointer allocate(Alloc& a, size_type n); [[nodiscard]] static pointer allocate(Alloc& a, size_type n, const_void_pointer hint); static void deallocate(Alloc& a, pointer p, size_type n); template<class T, class... Args> static void construct(Alloc& a, T* p, Args&&... args); template<class T> static void destroy(Alloc& a, T* p); static size_type max_size(const Alloc& a) noexcept; static Alloc select_on_container_copy_construction(const Alloc& rhs); }; }
类模板 std::allocator
namespace std { template<class T> class allocator { public: using value_type = T; using size_type = size_t; using difference_type = ptrdiff_t; using propagate_on_container_move_assignment = true_type; using is_always_equal = true_type; constexpr allocator() noexcept; constexpr allocator(const allocator&) noexcept; template<class U> constexpr allocator(const allocator<U>&) noexcept; ~allocator(); allocator& operator=(const allocator&) = default; [[nodiscard]] T* allocate(size_t n); void deallocate(T* p, size_t n); }; }
类模板 std::default_delete
namespace std { template<class T> struct default_delete { constexpr default_delete() noexcept = default; template<class U> default_delete(const default_delete<U>&) noexcept; void operator()(T*) const; }; template<class T> struct default_delete<T[]> { constexpr default_delete() noexcept = default; template<class U> default_delete(const default_delete<U[]>&) noexcept; template<class U> void operator()(U* ptr) const; }; }
类模板 std::unique_ptr
namespace std { template<class T, class D = default_delete<T>> class unique_ptr { public: using pointer = /* 见定义 */; using element_type = T; using deleter_type = D; // 构造函数 constexpr unique_ptr() noexcept; explicit unique_ptr(pointer p) noexcept; unique_ptr(pointer p, /* 见定义 */ d1) noexcept; unique_ptr(pointer p, /* 见定义 */ d2) noexcept; unique_ptr(unique_ptr&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept; // 析构函数 ~unique_ptr(); // 赋值 unique_ptr& operator=(unique_ptr&& u) noexcept; template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; unique_ptr& operator=(nullptr_t) noexcept; // 探查函数 add_lvalue_reference_t<T> operator*() const; pointer operator->() const noexcept; pointer get() const noexcept; deleter_type& get_deleter() noexcept; const deleter_type& get_deleter() const noexcept; explicit operator bool() const noexcept; // 修改函数 pointer release() noexcept; void reset(pointer p = pointer()) noexcept; void swap(unique_ptr& u) noexcept; // 禁用从左值复制 unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; }; template<class T, class D> class unique_ptr<T[], D> { public: using pointer = /* 见定义 */; using element_type = T; using deleter_type = D; // 构造函数 constexpr unique_ptr() noexcept; template<class U> explicit unique_ptr(U p) noexcept; template<class U> unique_ptr(U p, /* 见定义 */ d) noexcept; template<class U> unique_ptr(U p, /* 见定义 */ d) noexcept; unique_ptr(unique_ptr&& u) noexcept; template<class U, class E> unique_ptr(unique_ptr<U, E>&& u) noexcept; constexpr unique_ptr(nullptr_t) noexcept; // 析构函数 ~unique_ptr(); // 赋值 unique_ptr& operator=(unique_ptr&& u) noexcept; template<class U, class E> unique_ptr& operator=(unique_ptr<U, E>&& u) noexcept; unique_ptr& operator=(nullptr_t) noexcept; // 探察函数 T& operator[](size_t i) const; pointer get() const noexcept; deleter_type& get_deleter() noexcept; const deleter_type& get_deleter() const noexcept; explicit operator bool() const noexcept; // 修改函数 pointer release() noexcept; template<class U> void reset(U p) noexcept; void reset(nullptr_t = nullptr) noexcept; void swap(unique_ptr& u) noexcept; // 禁用从左值复制 unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; }; }
类 std::bad_weak_ptr
namespace std { class bad_weak_ptr : public exception { public: bad_weak_ptr() noexcept; }; }
namespace std { template<class T> class shared_ptr { public: using element_type = remove_extent_t<T>; using weak_type = weak_ptr<T>; // 构造函数 constexpr shared_ptr() noexcept; constexpr shared_ptr(nullptr_t) noexcept : shared_ptr() { } template<class Y> explicit shared_ptr(Y* p); template<class Y, class D> shared_ptr(Y* p, D d); template<class Y, class D, class A> shared_ptr(Y* p, D d, A a); template<class D> shared_ptr(nullptr_t p, D d); template<class D, class A> shared_ptr(nullptr_t p, D d, A a); template<class Y> shared_ptr(const shared_ptr<Y>& r, element_type* p) noexcept; template<class Y> shared_ptr(shared_ptr<Y>&& r, element_type* p) noexcept; shared_ptr(const shared_ptr& r) noexcept; template<class Y> shared_ptr(const shared_ptr<Y>& r) noexcept; shared_ptr(shared_ptr&& r) noexcept; template<class Y> shared_ptr(shared_ptr<Y>&& r) noexcept; template<class Y> explicit shared_ptr(const weak_ptr<Y>& r); template<class Y, class D> shared_ptr(unique_ptr<Y, D>&& r); // 析构函数 ~shared_ptr(); // 赋值 shared_ptr& operator=(const shared_ptr& r) noexcept; template<class Y> shared_ptr& operator=(const shared_ptr<Y>& r) noexcept; shared_ptr& operator=(shared_ptr&& r) noexcept; template<class Y> shared_ptr& operator=(shared_ptr<Y>&& r) noexcept; template<class Y, class D> shared_ptr& operator=(unique_ptr<Y, D>&& r); // 修改函数 void swap(shared_ptr& r) noexcept; void reset() noexcept; template<class Y> void reset(Y* p); template<class Y, class D> void reset(Y* p, D d); template<class Y, class D, class A> void reset(Y* p, D d, A a); // 探察函数 element_type* get() const noexcept; T& operator*() const noexcept; T* operator->() const noexcept; element_type& operator[](ptrdiff_t i) const; long use_count() const noexcept; explicit operator bool() const noexcept; template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept; template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept; }; template<class T> shared_ptr(weak_ptr<T>) -> shared_ptr<T>; template<class T, class D> shared_ptr(unique_ptr<T, D>) -> shared_ptr<T>; }
类模板 std::weak_ptr
namespace std { template<class T> class weak_ptr { public: using element_type = remove_extent_t<T>; // 构造函数 constexpr weak_ptr() noexcept; template<class Y> weak_ptr(const shared_ptr<Y>& r) noexcept; weak_ptr(const weak_ptr& r) noexcept; template<class Y> weak_ptr(const weak_ptr<Y>& r) noexcept; weak_ptr(weak_ptr&& r) noexcept; template<class Y> weak_ptr(weak_ptr<Y>&& r) noexcept; // 析构函数 ~weak_ptr(); // 赋值 weak_ptr& operator=(const weak_ptr& r) noexcept; template<class Y> weak_ptr& operator=(const weak_ptr<Y>& r) noexcept; template<class Y> weak_ptr& operator=(const shared_ptr<Y>& r) noexcept; weak_ptr& operator=(weak_ptr&& r) noexcept; template<class Y> weak_ptr& operator=(weak_ptr<Y>&& r) noexcept; // 修改函数 void swap(weak_ptr& r) noexcept; void reset() noexcept; // 探察函数 long use_count() const noexcept; bool expired() const noexcept; shared_ptr<T> lock() const noexcept; template<class U> bool owner_before(const shared_ptr<U>& b) const noexcept; template<class U> bool owner_before(const weak_ptr<U>& b) const noexcept; }; template<class T> weak_ptr(shared_ptr<T>) -> weak_ptr<T>; }
类模板 std::owner_less
namespace std { template<class T = void> struct owner_less; template<class T> struct owner_less<shared_ptr<T>> { bool operator()(const shared_ptr<T>&, const shared_ptr<T>&) const noexcept; bool operator()(const shared_ptr<T>&, const weak_ptr<T>&) const noexcept; bool operator()(const weak_ptr<T>&, const shared_ptr<T>&) const noexcept; }; template<class T> struct owner_less<weak_ptr<T>> { bool operator()(const weak_ptr<T>&, const weak_ptr<T>&) const noexcept; bool operator()(const shared_ptr<T>&, const weak_ptr<T>&) const noexcept; bool operator()(const weak_ptr<T>&, const shared_ptr<T>&) const noexcept; }; template<> struct owner_less<void> { template<class T, class U> bool operator()(const shared_ptr<T>&, const shared_ptr<U>&) const noexcept; template<class T, class U> bool operator()(const shared_ptr<T>&, const weak_ptr<U>&) const noexcept; template<class T, class U> bool operator()(const weak_ptr<T>&, const shared_ptr<U>&) const noexcept; template<class T, class U> bool operator()(const weak_ptr<T>&, const weak_ptr<U>&) const noexcept; using is_transparent = /* 未指明 */; }; }
namespace std { template<class T> class enable_shared_from_this { protected: constexpr enable_shared_from_this() noexcept; enable_shared_from_this(const enable_shared_from_this&) noexcept; enable_shared_from_this& operator=(const enable_shared_from_this&) noexcept; ~enable_shared_from_this(); public: shared_ptr<T> shared_from_this(); shared_ptr<T const> shared_from_this() const; weak_ptr<T> weak_from_this() noexcept; weak_ptr<T const> weak_from_this() const noexcept; private: mutable weak_ptr<T> weak_this; // 仅用于阐释 }; }
namespace std { template<class T> struct atomic<shared_ptr<T>> { using value_type = shared_ptr<T>; static constexpr bool is_always_lock_free = /* 由实现定义 */; bool is_lock_free() const noexcept; void store(shared_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; shared_ptr<T> load(memory_order order = memory_order::seq_cst) const noexcept; operator shared_ptr<T>() const noexcept; shared_ptr<T> exchange(shared_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; bool compare_exchange_weak(shared_ptr<T>& expected, shared_ptr<T> desired, memory_order success, memory_order failure) noexcept; bool compare_exchange_strong(shared_ptr<T>& expected, shared_ptr<T> desired, memory_order success, memory_order failure) noexcept; bool compare_exchange_weak(shared_ptr<T>& expected, shared_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; bool compare_exchange_strong(shared_ptr<T>& expected, shared_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; constexpr atomic() noexcept = default; atomic(shared_ptr<T> desired) noexcept; atomic(const atomic&) = delete; void operator=(const atomic&) = delete; void operator=(shared_ptr<T> desired) noexcept; private: shared_ptr<T> p; // 仅用于阐释 }; }
类模板 std::atomic 针对 std::weak_ptr 的特化
namespace std { template<class T> struct atomic<weak_ptr<T>> { using value_type = weak_ptr<T>; static constexpr bool is_always_lock_free = /* 由实现定义 */; bool is_lock_free() const noexcept; void store(weak_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; weak_ptr<T> load(memory_order order = memory_order::seq_cst) const noexcept; operator weak_ptr<T>() const noexcept; weak_ptr<T> exchange(weak_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; bool compare_exchange_weak(weak_ptr<T>& expected, weak_ptr<T> desired, memory_order success, memory_order failure) noexcept; bool compare_exchange_strong(weak_ptr<T>& expected, weak_ptr<T> desired, memory_order success, memory_order failure) noexcept; bool compare_exchange_weak(weak_ptr<T>& expected, weak_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; bool compare_exchange_strong(weak_ptr<T>& expected, weak_ptr<T> desired, memory_order order = memory_order::seq_cst) noexcept; constexpr atomic() noexcept = default; atomic(weak_ptr<T> desired) noexcept; atomic(const atomic&) = delete; void operator=(const atomic&) = delete; void operator=(weak_ptr<T> desired) noexcept; private: weak_ptr<T> p; // 仅用于阐释 }; }