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
1
vote
2 answers

Assigning unique_ptr vector to vector c++

I'm currently trying to incorporate dynamic footstep audio into my game. Heres some code for now: class MyClass { vector< unique_ptr > footstep_a; vector< unique_ptr > footstep_b; vector< unique_ptr > footstep_c; …
user2990037
  • 397
  • 5
  • 12
1
vote
3 answers

Does C++ have a smart pointer like unique_ptr with a "destruct before constructing" semantics?

Problem Consider this simple class: struct C { C(const char* name) : name(name) { cout << "constructing " << name << endl; } ~C() { cout << "destructing " << name << endl; } string name; }; I would like to…
WaelJ
  • 2,942
  • 4
  • 22
  • 28
1
vote
5 answers

C++ std::unique_ptr in std::map

Is this: std::unique_ptr texture; texture = std::move(textureMap.at(id)); return *texture; the same like this? auto found = textureMap.find(id); return *found->second; textureMap is: std::map> If…
Majak
  • 1,543
  • 1
  • 14
  • 28
1
vote
3 answers

Object still accessible after std::unique_ptr goes out of scope. Differing runtime behaviours

The following code passes to the function modify_entry a pointer to an object of type Entry and inside the body of the function a unique_ptr adopts the raw pointer. However, the object to which the pointers point seems to live on after the function…
Drake
  • 857
  • 5
  • 10
1
vote
0 answers

Why can't I move unique_ptr out of multiset while compiler is happy to do it with vector?

The simple code below would not compile using Clang on Mac, void foo(std::vector > &fromVector, std::multiset > &fromMultiset) { std::vector > to; …
LRaiz
  • 667
  • 7
  • 18
1
vote
2 answers

std::vector > does not compile

When I declare a vector of unique_ptr's, I get this kind of error: d:\qt\mingw64\include\c++\4.8.0\bits\stl_construct.h:75: error: use of deleted function 'std::unique_ptr<_Tp, _Dp>::unique_ptr( const std::unique_ptr<_Tp, _Dp>&) [with _Tp = int; _Dp…
galinette
  • 8,896
  • 2
  • 36
  • 87
1
vote
3 answers

How to use unique_ptr with data allocated within a c library?

I'm in c++ and i'm writing a wrapper around a c library. The c library function i'm working with takes a type 'LDAPMessage **' as a parameter, where it will allocate the memory for you within the function. Normal usage looks like this: LDAPMessage *…
user712850
1
vote
2 answers

nested unique_ptr and stl containers

I have recently read about RAII and have begun using it. I am trying to define graph as adjacency list and allocate the entire DS on heap using unique_ptr. I know that I can define them as stack objects, but I am trying to use unique_ptr's to get…
claudius
  • 1,112
  • 1
  • 10
  • 23
1
vote
1 answer

how to make a std list of a class that contains unique pointers

I'm trying to make a std list that will hold a list of classes that contain a unique pointer. I keep getting an error that says attempting to reference a deleted function. I understand that the problem is that unique pointers don't have an…
Lucas
  • 567
  • 1
  • 8
  • 21
1
vote
2 answers

std::unique_ptr without allocating memory on heap

I wrote accidentally a statement like std::unique_ptr m_thing; m_thing->DoStuff(); instead of std::unique_ptr m_thing(new Thing); m_thing->DoStuff(); the first example compiles and run, which doesn't make any sense to me because…
Grundkurs
  • 199
  • 1
  • 8
1
vote
1 answer

How to use std::unique_ptr for a std::list variable

I have a consumer-producer situation where I am constantly pushing data into a list, if it doesn't already exist there, and then every few seconds I package and send the data to a server. At the moment I have a thread that delays for some number of…
James Black
  • 41,583
  • 10
  • 86
  • 166
1
vote
1 answer

Is custom deleter for std::unique_ptr a valid place for manual call to destructor?

I have a very basic implementation of reflection that includes a Type class which does object instantiation for the class it describes. Stripped down to the relevant parts, it looks like this: Type.h: class Plugin; // forward declaration typedef…
d7samurai
  • 3,086
  • 2
  • 30
  • 43
1
vote
1 answer

How to implement move semantics in a template class

I have a template class for a queue. I also would like be able to store objects contained in a std::unique_ptr. Normal template specialization is not possible because the std::unique pointer can be instantiated with any type. Queue code is like…
Waldorf
  • 833
  • 2
  • 13
  • 24
1
vote
3 answers

Defining a unique pointer in a header file with a custom deleter defined externally

I am converting my code to use unique_ptr instead of just pointers. I am trying to create a unique_ptr to a sqlite3_statement that automatically calls the function sqlite3_finalize(sqlite3_stmt *pStmt) in its custom deleter. How can I get this to…
Aktaeon
  • 189
  • 2
  • 14
1
vote
3 answers

compilation error when using std::unique_ptr as value in tbb::concurrent_hash_map

I'm trying to replace a std::unordered_map with a tbb::concurrent_hash_map. My original code: typedef std::unique_ptr V_ptr; std::unordered_map hm; V_ptr v (new V); K k; hm.insert (std::make_pair (k, std::move (v))); compiles fine…
scmcduffee
  • 65
  • 4