Questions tagged [unique-ptr]

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer. unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object.

cppreference:

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.

std::unique_ptr was designed to replace the std::auto_ptr in C++03. It improves on the implementation of auto_ptr by implementing specific move semantics (it is not copyable) that were not available in the language of C++03.

std::unique_ptr, together with std::shared_ptr and (std::weak_ptr) form the core smart pointers used in C++ to implement RAII semantics, especially with respect to traditional memory management. With custom deleters, these smart pointers can also be used to manage other resources.

Resources:

2193 questions
83
votes
10 answers

Why can't a weak_ptr be constructed from a unique_ptr?

If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of which is managed by someone else. So I don't really see…
notadam
  • 2,754
  • 2
  • 19
  • 35
78
votes
2 answers

Why does unique_ptr take two template parameters when shared_ptr only takes one?

Both unique_ptr and shared_ptr accept a custom deleter to call on the object they own. But in the case of unique_ptr, the deleter is passed as a template parameter of the class, whereas the type of shared_ptr's custom deleter is to be specified as a…
qdii
  • 12,505
  • 10
  • 59
  • 116
76
votes
3 answers

C++ inserting unique_ptr in map

I have a C++ object of type ObjectArray typedef map> ObjectArray; What is the syntax to create a unique_ptr to a new object of type Class1 and insert it into an object of type ObjectArray?
vigs1990
  • 1,639
  • 4
  • 17
  • 19
73
votes
1 answer

Iterating through vector> using C++11 for() loops

I've got the following batch of code: std::vector> AVLArray(100000); /* Let's add some objects in the vector */ AVLTree_GeeksforGeeks *avl = new AVLTree_GeeksforGeeks(); avl->Insert[2]; avl->Insert[5];…
Dimitris Sfounis
  • 2,400
  • 4
  • 31
  • 46
70
votes
1 answer

"Downcasting" unique_ptr to unique_ptr

I have a series of factories that return unique_ptr. Under the hood, though, they are providing pointers to various derived types, i.e unique_ptr, unique_ptr, unique_ptretc. Given DerivedA : Derived and Derived :…
d7samurai
  • 3,086
  • 2
  • 30
  • 43
69
votes
2 answers

How do I use unique_ptr for pimpl?

Here is a simplification of what I'm seeing when I try to use unique_ptr for pimpl. I chose unique_ptr because I really want the class to own the pointer - I want the lifetimes of the pimpl pointer and the class to be the same. Anyway, here is the…
emsr
  • 15,539
  • 6
  • 49
  • 62
67
votes
3 answers

What happens to unique_ptr after std::move()?

This code is what I want to do: Tony& Movie::addTony() { Tony *newTony = new Tony; std::unique_ptr tony(newTony); attachActor(std::move(tony)); return *newTony; } I am wondering if I could do this instead: Tony&…
user3496846
  • 1,627
  • 3
  • 16
  • 28
64
votes
2 answers

Difference between boost::scoped_ptr and std::unique_ptr

Is the sole difference between boost::scoped_ptr and std::unique_ptr the fact that std::unique_ptr has move semantics whereas boost::scoped_ptr is just a get/reset smart pointer?
moshbear
  • 3,282
  • 1
  • 19
  • 33
64
votes
3 answers

error: ‘unique_ptr’ is not a member of ‘std’

I guess it's pretty self explanatory - I can't seem to use C++11 features, even though I think I have everything set up properly - which likely means that I don't. Here's my code: #include #include class Object { private: …
stellarossa
  • 1,730
  • 1
  • 18
  • 29
61
votes
1 answer

remove unique_ptr from queue

I'm trying to figure out how/if I can use unique_ptr in a queue. // create queue std::queue> q; // add element std::unique_ptr p (new int{123}); q.push(std::move(p)); // try to grab the element auto p2 =…
Ilia Choly
  • 18,070
  • 14
  • 92
  • 160
59
votes
9 answers

shared_ptr<> is to weak_ptr<> as unique_ptr<> is to... what?

In C++11, you can use a shared_ptr<> to establish an ownership relation with an object or variable and weak_ptr<> to safely reference that object in a non-owned way. You can also use unique_ptr<> to establish an ownership relation with an object or…
OldPeculier
  • 11,049
  • 13
  • 50
  • 76
57
votes
4 answers

How does the custom deleter of std::unique_ptr work?

According to N3290, std::unique_ptr accepts a deleter argument in its constructor. However, I can't get that to work with Visual C++ 10.0 or MinGW g++ 4.4.1 in Windows, nor with g++ 4.6.1 in Ubuntu. I therefore fear that my understanding of it is…
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
56
votes
5 answers

unique_ptr boost equivalent?

Is there some equivalent class for C++1x's std::unique_ptr in the boost libraries? The behavior I'm looking for is being able to have an exception-safe factory function, like so... std::unique_ptr create_base() { return…
Clark Gaebel
  • 17,280
  • 20
  • 66
  • 93
56
votes
9 answers

Dynamic casting for unique_ptr

As it was the case in Boost, C++11 provides some functions for casting shared_ptr: std::static_pointer_cast std::dynamic_pointer_cast std::const_pointer_cast I am wondering, however, why there are no equivalents functions for unique_ptr. Consider…
betabandido
  • 18,946
  • 11
  • 62
  • 76
54
votes
6 answers

How to capture a unique_ptr into a lambda expression?

I have tried the following: std::function getAction(std::unique_ptr &&psomething){ //The caller given ownership of psomething return [psomething](){ psomething->do_some_thing(); //psomething is expected to…
Earth Engine
  • 10,048
  • 5
  • 48
  • 78