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
54
votes
4 answers

Should I use shared_ptr or unique_ptr

I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr. I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively…
Clinton
  • 22,361
  • 15
  • 67
  • 163
52
votes
4 answers

C++ std::unique_ptr return from function and test for null

I have a function that needs to return a pointer to an object of class myClass. For this purpose I´m using std::unique_ptr. If the function succeeds, it shall return a pointer to a object with data. If it fails, it should return null. This is my…
Mendes
  • 17,489
  • 35
  • 150
  • 263
51
votes
1 answer

Recommended usage of std::unique_ptr

What are recommended uses of a std::unique_ptr as to specifically where, when, and how is it is best used? I discovered: About unique_ptr performances I already know: std::unique_ptr was developed in C++11 as a replacement for std::auto_ptr That…
Mushy
  • 2,535
  • 10
  • 33
  • 54
50
votes
6 answers

Raw pointer lookup for sets of unique_ptrs

I often find myself wanting to write code like this: class MyClass { public: void addObject(std::unique_ptr&& newObject); void removeObject(const Object* target); private: std::set> objects; }; However, much…
jbatez
  • 1,772
  • 14
  • 26
49
votes
3 answers

Bad practice to return unique_ptr for raw pointer like ownership semantics?

I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a…
Bret Kuhns
  • 4,034
  • 5
  • 31
  • 43
49
votes
3 answers

Lock-free swap of two unique_ptr

Swapping two unique_ptrs is not guaranteed to be threadsafe. std::unique_ptr a, b; std::swap(a, b); // not threadsafe Since I need atomic pointer swaps and since I like the ownership handling of unique_ptr, is there a simple way to combine them…
user1181282
  • 766
  • 1
  • 7
  • 11
47
votes
5 answers

Passing unique_ptr to functions

I'm trying to "modernize" some existing code. I have a class which currently has a member variable "Device* device_". It uses new to create an instance in some initialization code and has a "delete device_" in the destructory. Member functions…
jcoder
  • 29,554
  • 19
  • 87
  • 130
47
votes
4 answers

How to perform a dynamic_cast with a unique_ptr?

I have a class hierarchy as follows: class BaseSession : public boost::enable_shared_from_this class DerivedSessionA : public BaseSession class DerivedSessionB : public BaseSession Within the derived class functions, I regularly call…
Sharath
  • 1,627
  • 2
  • 18
  • 34
47
votes
2 answers

Converting std::unique_ptr to std::unique_ptr

Using C++11, let's say I have factory functions dealing with base and derived classes: #include using namespace std; struct B { virtual ~B() {} }; struct D : B {}; unique_ptr MakeB() { auto b = unique_ptr( new B() ); return…
metal
  • 6,202
  • 1
  • 34
  • 49
46
votes
3 answers

So can unique_ptr be used safely in stl collections?

I am confused with unique_ptr and rvalue move philosophy. Let's say we have two collections: std::vector> autoCollection; std::vector> uniqueCollection; Now I would expect the following to fail, as there is…
DanDan
  • 10,462
  • 8
  • 53
  • 69
46
votes
2 answers

How to initialize std::unique_ptr in constructor?

A.hpp: class A { private: std::unique_ptr file; public: A(std::string filename); }; A.cpp: A::A(std::string filename) { this->file(new std::ifstream(filename.c_str())); } The error that I get is thrown: A.cpp:7:43:…
user1529891
44
votes
3 answers

Alternatives of static_pointer_cast for unique_ptr

I understand that using static_pointer_cast with unique_ptr would lead to a shared ownership of the contained data. In other terms, what I'd like to do is: unique_ptr foo = fooFactory(); // do something for a while unique_ptr bar =…
skypjack
  • 49,335
  • 19
  • 95
  • 187
44
votes
4 answers

C++ std::unique_ptr : Why isn't there any size fees with lambdas?

I am reading "Effective Modern C++". In the item related to std::unique_ptr it's stated that if the custom deleter is a stateless object, then no size fees occur, but if it's a function pointer or std::function size fee occurs. Could you explain…
jnbrq -Canberk Sönmez
  • 1,790
  • 1
  • 17
  • 29
43
votes
6 answers

How to assign the address of an existing object to a smart pointer?

#include class bar{}; void foo(bar &object){ std::unique_ptr pointer = &object; } I want to assign an address of the object to the pointer. The above code obviously wont compile, because the right side of the assignment operator…
Tomasz Kasperczyk
  • 1,991
  • 3
  • 22
  • 43