Namespaces
Variants

std::auto_ptr<T>:: auto_ptr

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
explicit auto_ptr ( X * p = 0 ) throw ( ) ;
(1) (C++11 中弃用)
(C++17 中移除)
auto_ptr ( auto_ptr & r ) throw ( ) ;
(2) (C++11 中弃用)
(C++17 中移除)
template < class Y >
auto_ptr ( auto_ptr < Y > & r ) throw ( ) ;
(3) (C++11 中弃用)
(C++17 中移除)
auto_ptr ( auto_ptr_ref < X > m ) throw ( ) ;
(4) (C++11 中弃用)
(C++17 中移除)

从指向待管理对象的指针构造 auto_ptr

1) 使用指针 p 构造 auto_ptr
2) 使用 r 持有的指针构造 auto_ptr 。调用 r. release ( ) 来获取对象的所有权。
3) 与(2)相同。 Y * 必须能隐式转换为 T *
4) 使用由 m 引用的 auto_ptr 实例中持有的指针构造 auto_ptr 。对 m 持有的 auto_ptr p 调用 p. release ( ) 以获取对象的所有权。
auto_ptr_ref 是一个实现定义的类型,用于持有对 auto_ptr 的引用。 std::auto_ptr 可隐式 转换为 此类型,并可从此类型 赋值获得 。实现允许为该模板提供不同的名称,或以其他方式实现等效功能。

参数

p - 指向要管理的对象的指针
r - 另一个 auto_ptr ,用于从中转移对象所有权
m - 实现定义的类型,用于持有对 auto_ptr 的引用

注释

提供来自 auto_ptr_ref 的构造函数和复制赋值运算符,是为了允许从无名临时对象复制构造和赋值 std::auto_ptr 。由于其复制构造函数和复制赋值运算符将参数作为非常量引用接收,它们无法直接绑定右值参数。但是,可以执行 用户定义转换 (这会释放原始 auto_ptr ),随后调用按值接收 auto_ptr_ref 的构造函数或复制赋值运算符。这是 移动语义 的早期实现。