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:

1871 questions
65
votes
7 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,494
  • 2
  • 16
  • 30
64
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,379
  • 4
  • 12
  • 18
62
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
  • 13,551
  • 6
  • 45
  • 59
62
votes
6 answers

what's the point of std::unique_ptr::get

Doesn't std::unique_ptr::get defeat the purpose of having a unique_ptr in the first place? I would have expected this function to change its state so it holds no more pointer. Is there an actual useful use of std::unique_ptr::get?
lezebulon
  • 6,685
  • 8
  • 35
  • 66
61
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
  • 2,868
  • 2
  • 27
  • 39
60
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,162
  • 1
  • 17
  • 32
59
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
  • 16,442
  • 12
  • 75
  • 145
58
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,630
  • 1
  • 18
  • 29
57
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,142
  • 4
  • 27
  • 46
56
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
  • 135,616
  • 15
  • 192
  • 304
55
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
  • 15,590
  • 16
  • 61
  • 89
55
votes
8 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
  • 9,811
  • 8
  • 42
  • 71
53
votes
4 answers

Should I use shared_ptr or unique_ptr

I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr. I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively…
Clinton
  • 20,364
  • 13
  • 59
  • 142
50
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
  • 16,863
  • 10
  • 53
  • 69
49
votes
3 answers

Bad practice to return unique_ptr for raw pointer like ownership semantics?

I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a…
Bret Kuhns
  • 3,814
  • 5
  • 27
  • 42