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

How to pass const pointer to const object using unique_ptr

I want to pass a unique_ptr to a helper function, and I want to make sure that the helper function neither modifies the pointer, nor the pointed object. Without the unique_ptr, the solution is to have void takePtr(AClass const * const aPtr) { //…
Yogeshwer Sharma
  • 3,647
  • 5
  • 25
  • 27
30
votes
2 answers

Why is unique_ptr(T*) explicit?

The following functions do not compile: std::unique_ptr foo() { int* answer = new int(42); return answer; } std::unique_ptr bar() { return new int(42); } I find this a bit inconvenient. What was the rationale for making…
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
30
votes
2 answers

Does the standard behavior for deleters differ between shared_ptr and unique_ptr in the case of null pointers?

OK, so first some things that might be relevant: I'm using the Clang 3.1 compiler, in C++11 mode, with the standard library set to libc++. I'm trying to familiarize myself with C++11, and in so doing I ran across behavior that seems odd. It may be a…
Bob Miller
  • 603
  • 5
  • 12
29
votes
3 answers

make_unique with brace initialization

https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique writes that std::make_unique can be implemented as template std::unique_ptr make_unique(Args&&... args) { return std::unique_ptr(new…
MvG
  • 57,380
  • 22
  • 148
  • 276
29
votes
2 answers

Storing a unique_ptr with custom deleter in a map

Why doesn't this work? #include #include void deleter(int* i) { delete i; } std::map> m; void foo(int* i) { m[0] = std::unique_ptr(i,…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
29
votes
5 answers

Should I use shared_ptr or unique_ptr?

I have a question about std::unique_ptr and std::shared_ptr. I know there are loads of questions about when to use which one, but I'm still not sure if I understand it correctly. I read somewhere that the default choice for smart pointer should be…
dziwna
  • 1,212
  • 2
  • 14
  • 25
28
votes
3 answers

Why am I allowed to copy unique_ptr?

Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this : // disable copy from lvalue unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; So,…
BЈовић
  • 62,405
  • 41
  • 173
  • 273
28
votes
4 answers

How can unique_ptr have no overhead if it needs to store the deleter?

First take a look at what C++ Primer said about unique_ptr and shared_ptr: $16.1.6. Efficiency and Flexibility We can be certain that shared_ptr does not hold the deleter as a direct member, because the type of the deleter isn’t known until run…
choxsword
  • 3,187
  • 18
  • 44
28
votes
2 answers

Move a unique_ptr with custom deleter to a shared_ptr

I have a function which creates a unique_ptr with a custom deleter and returns it: auto give_unique_ptr() { auto deleter = [](int* pi) { delete pi; }; int* i = new int{1234}; return std::unique_ptr(i,…
j00hi
  • 5,420
  • 3
  • 45
  • 82
28
votes
1 answer

Deleter type in unique_ptr vs. shared_ptr

I thought it is very curious when I discovered that the standard defines std::unique_ptr and std::shared_ptr in two totally different ways regarding a Deleter that the pointer may own. Here is the declaration from cppreference::unique_ptr and…
WorldSEnder
  • 4,875
  • 2
  • 28
  • 64
28
votes
4 answers

How do I pass smart pointers into functions?

When passing objects into functions, do the same rules apply to smart pointers as to other objects that contain dynamic memory? When I pass, for example, a std::vector into a function I always consider the following options: I'm…
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
27
votes
4 answers

Should std::unique_ptr be permitted

This is a very simple question. Consider the following code: #include #include typedef std::unique_ptr UniqueVoidPtr; int main() { UniqueVoidPtr p(new int); return 0; } Compiling with cygwin (g++ 4.5.3) with the…
Andrew Falanga
  • 2,274
  • 4
  • 26
  • 51
26
votes
5 answers

Overload method for unique_ptr and shared_ptr is ambiguous with polymorphism

Coding stuff after taking the hint from my previous question's answer, I ran into an issue with overloading Scene::addObject. To reiterate the relevant bits and make this self contained, with the least details possible: I have a hierarchy of…
aPonza
  • 492
  • 4
  • 10
26
votes
2 answers

Pimpl - Why can make_unique be called on an incomplete type

Why does the make_unique call compile? Doesn't make_unqiue require its template argument to be a complete type ? struct F; int main() { std::make_unique(); } struct F {}; The question orignated from my "problem" with my PIMPL…
AF_cpp
  • 568
  • 5
  • 15
26
votes
1 answer

std::unique_ptr vs std::shared_ptr vs std::weak_ptr vs std::auto_ptr vs raw pointers

What are the equivalent uses of each smart pointer in comparison to similar (but not limited to) some advanced techniques using raw pointers? My understanding is minimal, but from what I can gather: Raw Pointers: Only use if you really, really,…
Casey
  • 10,297
  • 11
  • 59
  • 88