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

How do I pass a unique_ptr argument to a constructor or a function?

I'm new to move semantics in C++11 and I don't know very well how to handle unique_ptr parameters in constructors or functions. Consider this class referencing itself: #include class Base { public: typedef unique_ptr UPtr; …
codablank1
  • 5,425
  • 4
  • 17
  • 26
405
votes
6 answers

Returning unique_ptr from functions

unique_ptr does not allow copy construction, instead it supports move semantics. Yet, I can return a unique_ptr from a function and assign the returned value to a variable. #include #include using namespace…
Praetorian
  • 100,267
  • 15
  • 224
  • 307
270
votes
9 answers

Is std::unique_ptr required to know the full definition of T?

I have some code in a header that looks like this: #include class Thing; class MyClass { std::unique_ptr< Thing > my_thing; }; If I include this header in a cpp that does not include the Thing type definition, then this does not…
Klaim
  • 60,771
  • 31
  • 121
  • 186
269
votes
4 answers

Differences between unique_ptr and shared_ptr

Possible Duplicates: pimpl: shared_ptr or unique_ptr smart pointers (boost) explained Could someone explain differences between shared_ptr and unique_ptr?
smallB
  • 14,808
  • 29
  • 97
  • 144
267
votes
17 answers

Is there any use for unique_ptr with array?

std::unique_ptr has support for arrays, for instance: std::unique_ptr p(new int[10]); but is it needed? probably it is more convenient to use std::vector or std::array. Do you find any use for that construct?
fen
  • 8,812
  • 5
  • 29
  • 53
241
votes
2 answers

Why can I not push_back a unique_ptr into a vector?

What is wrong with this program? #include #include int main() { std::vector> vec; int x(1); std::unique_ptr ptr2x(&x); vec.push_back(ptr2x); //This tiny command has a vicious error. …
user383352
  • 4,443
  • 4
  • 22
  • 20
221
votes
6 answers

std::unique_ptr with an incomplete type won't compile

I'm using the pimpl-idiom with std::unique_ptr: class window { window(const rectangle& rect); private: class window_impl; // defined elsewhere std::unique_ptr impl_; // won't compile }; However, I get a compile error regarding…
user1203803
220
votes
6 answers

make_unique and perfect forwarding

Why is there no std::make_unique function template in the standard C++11 library? I find std::unique_ptr p(new SomeUserDefinedType(1, 2, 3)); a bit verbose. Wouldn't the following be much nicer? auto p =…
fredoverflow
  • 237,063
  • 85
  • 359
  • 638
195
votes
4 answers

std::auto_ptr to std::unique_ptr

With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr. Does their usage exactly overlap (so I can do a global find/replace on my code (not that…
Martin York
  • 234,851
  • 74
  • 306
  • 532
169
votes
2 answers

Using smart pointers for class members

I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr/weak_ptr work in general. What I don't understand is the real…
michaelk
  • 1,947
  • 3
  • 14
  • 18
149
votes
4 answers

Differences between std::make_unique and std::unique_ptr with new

Does std::make_unique have any efficiency benefits like std::make_shared? Compared to manually constructing std::unique_ptr: std::make_unique(1); // vs std::unique_ptr(new int(1));
NFRCR
  • 4,606
  • 4
  • 29
  • 34
147
votes
7 answers

How do I use a custom deleter with a std::unique_ptr member?

I have a class with a unique_ptr member. class Foo { private: std::unique_ptr bar; ... }; The Bar is a third party class that has a create() function and a destroy() function. If I wanted to use a std::unique_ptr with it in a stand…
huitlarc
  • 1,733
  • 2
  • 11
  • 12
133
votes
2 answers

Advantages of using std::make_unique over new operator

What are the advantages of using std::make_unique over the new operator for initializing a std::unique_ptr? In other words, why is std::unique_ptr a = std::make_unique(SomeObject(...)) better than doing std::unique_ptr a =…
niting
  • 1,854
  • 3
  • 17
  • 20
121
votes
6 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
  • 8,016
  • 11
  • 44
  • 70
116
votes
7 answers

How can I pass std::unique_ptr into a function

How can I pass a std::unique_ptr into a function? Lets say I have the following class: class A { public: A(int val) { _val = val; } int GetVal() { return _val; } private: int _val; }; The following does not…
user3690202
  • 3,264
  • 3
  • 19
  • 35
1
2 3
99 100