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
488
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
  • 6,055
  • 5
  • 19
  • 29
485
votes
7 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
  • 106,671
  • 19
  • 240
  • 328
342
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
  • 16,662
  • 33
  • 107
  • 151
311
votes
18 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
  • 9,835
  • 5
  • 34
  • 57
302
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
  • 67,274
  • 36
  • 133
  • 188
284
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
  • 5,033
  • 4
  • 25
  • 20
262
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
226
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
  • 256,549
  • 94
  • 388
  • 662
211
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
  • 257,169
  • 86
  • 333
  • 562
190
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
  • 5,304
  • 6
  • 32
  • 37
186
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
  • 2,384
  • 3
  • 19
  • 20
185
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
  • 2,107
  • 3
  • 15
  • 18
173
votes
10 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,993
  • 2
  • 13
  • 12
164
votes
8 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,844
  • 3
  • 22
  • 36
149
votes
3 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
  • 9,717
  • 13
  • 63
  • 119
1
2 3
99 100