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
42
votes
2 answers

Iterating over a container of unique_ptr's

How does one access unique_ptr elements of a container (via an iterator) without taking ownership away from the container? When one gets an iterator to an element in the container is the element ownership still with the container? How about when one…
Dr DR
  • 667
  • 1
  • 5
  • 8
41
votes
3 answers

std::unique_ptr usage

std::unique_ptr p1(new int); std::unique_ptr 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…
Guillaume Paris
  • 10,303
  • 14
  • 70
  • 145
41
votes
1 answer

How to make std::make_unique a friend of my class

I want to declare std::make_unique function as a friend of my class. The reason is that I want to declare my constructor protected and provide an alternative method of creating the object using unique_ptr. Here is a sample code: #include…
TheCrafter
  • 1,909
  • 2
  • 23
  • 44
41
votes
2 answers

Calling initializer_list constructor via make_unique/make_shared

I'm trying to use std::make_unique to instanciate a class whose constructor is to receive an std::initializer_list. Here a minimal case : #include #include #include #include struct Foo { …
Quentin
  • 62,093
  • 7
  • 131
  • 191
40
votes
3 answers

Is unique_ptr thread safe?

Is unique_ptr thread safe? Is it impossible for the code below to print same number twice? #include #include #include #include using namespace std; int main() { unique_ptr work; thread t1([&] { …
Valentin Milea
  • 3,186
  • 3
  • 28
  • 29
37
votes
2 answers

How to implement make_unique function in C++11?

My compiler doesn't support make_unique. How to write one? template< class T, class... Args > unique_ptr make_unique( Args&&... args );
user1899020
  • 13,167
  • 21
  • 79
  • 154
35
votes
4 answers

When moving a unique_ptr into a lambda, why is it not possible to call reset?

When moving std::unique_ptr into the lambda, it is not possible to call reset() on it, because it seems to be const then: error C2662: void std::unique_ptr>::reset(int *) noexcept': cannot convert 'this' pointer from…
Roi Danton
  • 7,933
  • 6
  • 68
  • 80
35
votes
2 answers

push_back or emplace_back with std::make_unique

Based on the answers in these questions here, I know that it is certainly preferred to use c++14's std::make_unique than to emplace_back(new X) directly. That said, is it preferred to call my_vector.push_back(std::make_unique("constructor",…
NHDaly
  • 7,390
  • 4
  • 40
  • 45
34
votes
6 answers

Using a std::unordered_set of std::unique_ptr

Assume I have a set of unique_ptr: std::unordered_set > my_set; I'm not sure what's the safe way to check if a given pointer exists in the set. The normal way to do it may be to call my_set.find (), but what do I pass as a…
34
votes
2 answers

unique_ptr lambda custom deleter for array specialization

I recently started porting lots of my existing C++ application code to over to C++11 and now that I am converting to the new smart pointers std::unique_ptr and std::shared_ptr, I have a specific question about custom deleters. I want to add a lambda…
johnco3
  • 2,401
  • 4
  • 35
  • 67
33
votes
2 answers

Performance of resizing std::vector>

The general conception seems to be that std::unique_ptr has no time overhead compared to properly used owning raw pointers, given sufficient optimization. But what about using std::unique_ptr in compound data structures, in particular…
Zulan
  • 21,896
  • 6
  • 49
  • 109
33
votes
4 answers

Initializing a std::unique_ptr by passing the address of the pointer

I am creating a class which interops with some Windows API code, now one of the pointers I have to initialize is done by calling a native function which initializes it. My pointers are of type std::unique_ptr with a custom deleter, which calls the…
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
32
votes
2 answers

Why is "error: invalid application of 'sizeof' to an incomplete type using unique_ptr" fixed by adding an empty destructor?

I am Pimpling off the class STFT. Compiles just fine with this in the header: class STFT; // pimpl off to prevent point name clash class Whatever { private: STFT* stft; and this in the implementation: #include "STFT.h" Whatever::Whatever() :…
learnvst
  • 15,455
  • 16
  • 74
  • 121
31
votes
1 answer

Proper way of transferring ownership of a std::vector< std::unique_ptr< int> > to a class being constructed

What is the proper way of transferring ownership of a std::vector > to a class being constructed? Below is a code representation of what I want to do. I realize it is not correct (won't compile) and violates "uniqueness" whether I…
Jen
  • 313
  • 1
  • 3
  • 6
30
votes
2 answers

Why is `make_unique` disallowed?

Assume namespace std throughout. The C++14 committee draft N3690 defines std::make_unique thus: [n3690: 20.9.1.4]: unique_ptr creation    [unique.ptr.create] template unique_ptr make_unique(Args&&... args); 1 Remarks:…
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055