Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::unique_ptr usage

std::unique_ptr<int> p1(new int); std::unique_ptr<int> p2(new int); p2=p1; 

It seems here that p1 is no longer "unique" since p2 refer to it also

It is legal c++ ? Does unique_ptr have copy_semantics ? If no, and if it has only move semantics, is p1 set to NULL after assign it to p2 ?

EDIT:

ok so the correct version is

 p2=std::move(p1) 

According to that, after this assign, p1 is not valid ? And the difference with auto_ptr is here? it is more safe to explictly specfiy transfer of ownership than implicitly as it is the case with auto_ptr I guess

like image 716
Guillaume Paris Avatar asked Apr 11 '11 14:04

Guillaume Paris


People also ask

What is the use of std :: unique_ptr?

std::unique_ptr is a smart pointer that owns and manages another object through a pointer and disposes of that object when the unique_ptr goes out of scope. The object is disposed of, using the associated deleter when either of the following happens: the managing unique_ptr object is destroyed.

How does C++ unique_ptr work?

unique_ptr<> is one of the Smart pointer implementation provided by c++11 to prevent memory leaks. A unique_ptr object wraps around a raw pointer and its responsible for its lifetime. When this object is destructed then in its destructor it deletes the associated raw pointer.

Should I use shared or unique_ptr?

Use unique_ptr when you want a single pointer to an object that will be reclaimed when that single pointer is destroyed. Use shared_ptr when you want multiple pointers to the same resource.

Do I need to delete unique_ptr?

An explicit delete for a unique_ptr would be reset() . But do remember that unique_ptr are there so that you don't have to manage directly the memory they hold. That is, you should know that a unique_ptr will safely delete its underlying raw pointer once it goes out of scope.


2 Answers

Here is an article I wrote which answers your questions. I originally wrote this article to present an emulation of unique_ptr. However you can ignore the first few paragraphs dealing with the emulation and just start reading at "Basic Examples".

http://howardhinnant.github.io/unique_ptr03.html

Edit:

I had trouble distilling the above linked article down to something small enough to make a practical answer in this format. However here is my best shot:

The reason: Safety in generic code. One can not really make copies of either auto_ptr or unique_ptr. Consider:

template <class T> void foo(T t) {     T copy_of_t = t;  // line 4     assert(copy_of_t == t); } 

It is not unusual at all for generic code to look like foo above. The assert is probably not actually there, but the assumption that the assert would hold often is there ... implicitly. Indeed, a popular implementation of std::sort had exactly this logic in 1996, which is exactly what prompted the second auto_ptr redesign (which helped, but didn't completely fix the problem).

like image 39
Howard Hinnant Avatar answered Oct 04 '22 15:10

Howard Hinnant


std::unique_ptr is non-assignable and non-copyable. You need to use std::move();

so

p1 = std::move(p2); 

Have a look here for more info.

like image 190
Tony The Lion Avatar answered Oct 04 '22 15:10

Tony The Lion