std::auto_ptr<T>::operator=
auto_ptr& operator=( auto_ptr& r ) throw(); (1)
template< class Y >
auto_ptr& operator=( auto_ptr<Y>& r ) throw(); (2)
auto_ptr& operator=( auto_ptr_ref<T> m ) throw(); (3)
用 r 或 m 所管理的对象替换当前所管理的对象。
-
有效地调用
reset(r.release())
。 -
有效地调用
reset(r.release())
。Y*
必须可隐式转换为T*
。 -
有效地调用
reset(m.release())
。auto_ptr_ref 是一个实现定义的类型,它持有对 auto_ptr 的引用。std::auto_ptr
可隐式地与此类型进行转换和被转换。实现允许提供具有不同名称的模板或以其他方式实现等效功能。
参数
r
- 另一个 auto_ptr,用于转移对象的所有权m
- 一个实现定义的类型对象,它持有对 auto_ptr 的引用
返回值
*this
.
备注
提供了从 auto_ptr_ref 的构造函数和复制赋值运算符,以允许从无名临时对象复制构造和赋值 std::auto_ptr。由于其复制构造函数和复制赋值运算符将参数作为非 const 引用,因此它们不能直接绑定 rvalue 参数。但是,可以执行用户定义的转换(它会释放原始的 auto_ptr),然后调用接受 auto_ptr_ref 按值的构造函数或复制赋值运算符。这是移动语义的早期实现。
缺陷报告
以下改变行为的缺陷报告已追溯应用于先前发布的 C++ 标准。
DR | 应用于 | 发布时的行为 | 正确行为 |
---|---|---|---|
LWG 127 | C++98 | auto_ptr 不能从 auto_ptr_ref 赋值 | 添加了重载 (3) |