Namespaces
Variants

std::auto_ptr<T>:: operator=

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)
auto_ptr & operator = ( auto_ptr & r ) throw ( ) ;
(1) (C++11 中已弃用)
(C++17 中移除)
template < class Y >
auto_ptr & operator = ( auto_ptr < Y > & r ) throw ( ) ;
(2) (C++11 中已弃用)
(C++17 中移除)
auto_ptr & operator = ( auto_ptr_ref < T > m ) throw ( ) ;
(3) (C++11 中已弃用)
(C++17 中移除)

将托管对象替换为由 r m 管理的对象。

1) 有效调用 reset ( r. release ( ) )
2) 实际上调用了 reset ( r. release ( ) ) Y* 必须能隐式转换为 T*
3) 实际上调用了 reset ( m. release ( ) ) auto_ptr_ref 是一个实现定义的类型,用于持有对 auto_ptr 的引用。 std::auto_ptr 可隐式 转换为 该类型,也可 从该类型转换 。允许实现为该模板提供不同名称,或以其他方式实现等效功能。

目录

参数

r - 用于转移对象所有权的另一个 auto_ptr
m - 持有 auto_ptr 引用的实现定义类型对象

返回值

* this

注释

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

缺陷报告

下列行为变更缺陷报告被追溯应用于先前发布的 C++ 标准。

缺陷报告 适用标准 发布时行为 正确行为
LWG 127 C++98 auto_ptr 无法从 auto_ptr_ref 赋值 添加重载 (3)