std::shared_ptr::operator= - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::shared_ptr::operator=

From cppreference.com
< cpp‎ | memory‎ | shared ptr

shared_ptr& operator=( const shared_ptr& r ); | (1) |
template< class Y > shared_ptr& operator=( const shared_ptr<Y>& r ); | (1) |
shared_ptr& operator=( shared_ptr&& r ); | (2) |
template< class Y > shared_ptr& operator=( shared_ptr<Y>&& r ); | (2) |
template< class Y > shared_ptr& operator=( std::auto_ptr<Y>&& r ); | (3) |
template< class Y, class Deleter > shared_ptr& operator=( std::unique_ptr<Y,Deleter>&& r ); | (4) |

Replaces the managed object with the one managed by `r`.

1) Shares ownership of the object managed by `r`. If `r` manages no object, `*this` manages no object too. The templated overload doesn't participate in the overload resolution if `Y*` is not implicitly convertible to `T*`. Equivalent to shared_ptr<T> p(r).swap(*this).

2) Move-assigns a `shared_ptr` from `r`. After the assignment, *this contains a copy of the previous state of `r`, `r` is empty. The templated overload doesn't participate in the overload resolution if `Y*` is not implicitly convertible to `T*`. Equivalent to shared_ptr<T> p(std::move(r)).swap(*this).

3) Transfers the ownership of the object managed by `r` to `*this`. If `r` manages no object, `*this` manages no object too. After the assignment, `*this` contains the pointer previously held by `r`, and use_count()==1; also `r` is empty. Equivalent to shared_ptr<T> p(r).swap(*this).

4) Transfers the ownership of the object managed by `r` to *this. The deleter associated to `r` is stored for future deletion of the managed object. `r` manages no object after the call. Equivalent to shared_ptr<T> p(std::move(r)).swap(*this).

### [edit] Parameters

r | - | another smart pointer to share the ownership to or acquire the ownership from

### [edit] Return value

*this

### [edit] Notes

The implementation may meet the requirements without creating a temporary `shared_ptr` object.

### [edit] Exceptions

1-2)
`noexcept` specification:
noexcept

3) (none)

4-5)
`noexcept` specification:
noexcept

### [edit] Example

| This section is incomplete Reason: no example

### [edit] See also

reset | replaces the managed object (public member function) [edit]

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/memory/shared_ptr/operator%3D&oldid=72091"
Categories:

- unconditionally noexcept
- Todo no example