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
114
votes
4 answers

How to declare std::unique_ptr and what is the use of it?

I try to understand how std::unique_ptr works and for that I found this document. The author starts from the following example: #include //declarations of unique_ptr using std::unique_ptr; // default construction unique_ptr up;…
Roman
  • 97,757
  • 149
  • 317
  • 426
112
votes
2 answers

Does C++11 unique_ptr and shared_ptr able to convert to each other's type?

Does C++11 standard library provide any utility to convert from a std::shared_ptr to std::unique_ptr, or vice versa? Is this safe operation?
Hind Forsum
  • 8,077
  • 8
  • 40
  • 88
107
votes
2 answers

Why is shared_ptr legal, while unique_ptr is ill-formed?

The question really fits in the title: I am curious to know what is the technical reason for this difference, but also the rationale ? std::shared_ptr sharedToVoid; // legal; std::unique_ptr uniqueToVoid; // ill-formed;
Ad N
  • 6,830
  • 4
  • 28
  • 65
103
votes
6 answers

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

I am trying to compile the following thread pool program posted on code review to test it. https://codereview.stackexchange.com/questions/55100/platform-independant-thread-pool-v4 But I am getting the errors threadpool.hpp: In member function…
Ali786
  • 4,211
  • 6
  • 34
  • 54
101
votes
6 answers

Why use std::make_unique in C++17?

As far as I understand, C++14 introduced std::make_unique because, as a result of the parameter evaluation order not being specified, this was unsafe: f(std::unique_ptr(new MyClass(param)), g()); // Syntax A (Explanation: if the evaluation…
Eternal
  • 2,303
  • 2
  • 11
  • 17
93
votes
4 answers

Why can a T* be passed in register, but a unique_ptr cannot?

I'm watching Chandler Carruth's talk in CppCon 2019: There are no Zero-Cost Abstractions in it, he gives the example of how he was surprised by just how much overhead you incur by using an std::unique_ptr over an int*; that segment starts about…
einpoklum
  • 86,754
  • 39
  • 223
  • 453
86
votes
4 answers

How to pass std::unique_ptr around?

I am having my first attempt at using C++11 unique_ptr; I am replacing a polymorphic raw pointer inside a project of mine, which is owned by one class, but passed around quite frequently. I used to have functions like: bool func(BaseClass* ptr, int…
lvella
  • 10,929
  • 10
  • 42
  • 91
84
votes
5 answers

Does unique_ptr::release() call the destructor?

Is this code correct? auto v = make_unique(12); v.release(); // is this possible? Is it equivalent to delete of a raw pointer?
Zeukis
  • 875
  • 1
  • 6
  • 8
84
votes
4 answers

Is auto_ptr deprecated?

Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership transfer instead of shared_ptr? If unique_ptr is not in the standard, then do I need to use shared_ptr instead?
dimba
  • 24,103
  • 28
  • 127
  • 188
83
votes
1 answer

Forward declaration with unique_ptr?

I have found it useful to use forward declaration of classes in combination with std::unique_ptr as in the code below. It compiles and works with GCC, but the whole thing seem kind of strange, and I wonder if this is standard behaviour (i.e.…
Zyx 2000
  • 1,445
  • 1
  • 11
  • 16
81
votes
3 answers

unique_ptr to a derived class as an argument to a function that takes a unique_ptr to a base class

I'm trying to use a unique_ptr to derived class in a function that takes a unique_ptr to a base class. Something like: class Base {}; class Derived : public Base {}; void f(unique_ptr const &base) {} … unique_ptr derived =…
svick
  • 214,528
  • 47
  • 357
  • 477
79
votes
2 answers

Is unique_ptr guaranteed to store nullptr after move?

Is unique_ptr guaranteed to store nullptr after move? std::unique_ptr p1{new int{23}}; std::unique_ptr p2{std::move(p1)}; assert(!p1); // is this always true?
lizarisk
  • 6,692
  • 9
  • 42
  • 66
78
votes
6 answers

Proper way to create unique_ptr that holds an allocated array

What is the proper way to create an unique_ptr that holds an array that is allocated on the free store? Visual studio 2013 supports this by default, but when I use gcc version 4.8.1 on Ubuntu I get memory leaks and undefined behaviour. The problem…
lauw
  • 1,031
  • 1
  • 9
  • 10
72
votes
2 answers

Should I assign or reset a unique_ptr?

Given the common situation where the lifespan of an owned object is linked to its owner, I can use a unique pointer one of 2 ways . . It can be assigned: class owner { std::unique_ptr owned; public: owner() { …
learnvst
  • 13,927
  • 13
  • 65
  • 108
69
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 destructor to call on the object they own. But in the case of unique_ptr, the destructor is passed as a template parameter of the class, whereas the type of shared_ptr's custom destructor is to be…
qdii
  • 11,387
  • 7
  • 54
  • 107