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
146
votes
7 answers

Copy constructor for a class with unique_ptr

How do I implement a copy constructor for a class that has a unique_ptr member variable? I am only considering C++11.
codefx
  • 9,872
  • 16
  • 53
  • 81
136
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
  • 124,451
  • 167
  • 349
  • 456
126
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
  • 1,295
  • 2
  • 9
  • 8
121
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
  • 7,930
  • 6
  • 36
  • 80
118
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,648
  • 2
  • 15
  • 21
118
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…
smali
  • 4,687
  • 7
  • 38
  • 60
107
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
  • 12,754
  • 11
  • 54
  • 106
106
votes
4 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
  • 236,525
  • 50
  • 385
  • 514
104
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
  • 118,144
  • 57
  • 340
  • 684
102
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,301
  • 2
  • 10
  • 10
98
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,625
  • 1
  • 12
  • 18
86
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
  • 26,717
  • 34
  • 141
  • 196
86
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
  • 15,455
  • 16
  • 74
  • 121
85
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
  • 7,562
  • 10
  • 46
  • 70
84
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
  • 7,607
  • 11
  • 42
  • 73