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
0 answers

vector copy constructible?

I am confused about: cout << is_copy_constructible::value << is_copy_constructible>::value << is_copy_constructible>>::value; I expected 100, but got 101 with gcc 4.9.1 and clang 3.4.2 How can…
idefixs
  • 712
  • 1
  • 4
  • 14
1
vote
1 answer

C++ :: two-dimensional matrix, dynamic in one dimension, using unique_ptr?

I have a large genetic dataset (X, Y coordinates), of which I can easily know one dimension (X) during runtime. I drafted the following code for a matrix class which allows to specify the size of one dimension, but leaves the other one dynamic by…
Stingery
  • 432
  • 4
  • 16
1
vote
2 answers

Sorting a vector of unique_ptr in VS 2013

I am attempting to make a Deck class that holds a vector of unique_ptr, but attempting to sort the vector results in this error: Error 1 error C2280: 'std::unique_ptr>:: unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>>…
1
vote
1 answer

Using initializer lists with a boost::multi_index::multi_index_container of std::unique_ptr elements

I'm getting compilation errors when trying to use an initializer list to assign values into a boost::multi_index::multi_index_container object containing std::unique_ptr elements of any type. Here's a short example (also available on…
Spire
  • 685
  • 6
  • 15
1
vote
2 answers

Unique pointer ownership

I am having difficulties understanding the ownership of unique pointers. Can anyone explain how it is possible to store a pointer to a unique pointer in a set? Example from SFML game dev book which illustrates the problem: typedef…
Tim Teige
  • 13
  • 2
1
vote
2 answers

Why does VC++ allow an instance of a template class without full template parameters?

In the VC++ 2013's C++ header file memory, I find the class unique_ptr is defined as follows: template // = default_delete<_Ty> class unique_ptr { ... }; What makes me confused is: the template parameter doesn't have a…
xmllmx
  • 39,765
  • 26
  • 162
  • 323
1
vote
0 answers

Strip pointer from void* for use in unique_ptr using statement?

I'm trying to compile a simple C++ program that uses some functions and datastructures from the Win32 API and Wincrypt: #include using std::unique_ptr; #include #include using CERTSTORE_ptr = unique_ptr
jww
  • 97,681
  • 90
  • 411
  • 885
1
vote
1 answer

Contents of list of unique_ptr are inaccessible

I've got a std::list of std::unique_ptrs to Entity objects. When I try to loop through them as such, the program says that the items within the list are inaccessible. The list is a member variable, declared as private: list< unique_ptr >. void…
JordanBell
  • 140
  • 1
  • 2
  • 11
1
vote
0 answers

Do I misunderstand unique_ptr or is it a CLang bug?

I have a class Foo - nothing special about it. A method of another class is returning a unique_ptr to a Foo. After a day of banging my head against my monitor I reduced the problem to the following: std::unique_ptr Other::method(float arg) { …
LRaiz
  • 667
  • 7
  • 18
1
vote
5 answers

Move vector> to a vector>

I have two classes, say Base and Derived: public Base and two std::vectors, one holding elements of type unique_ptr and other one holds elements of type unique_ptr How can I transfer ownership of all elements from second vector into…
sorush-r
  • 10,490
  • 17
  • 89
  • 173
1
vote
2 answers

How to delete raw pointers from a unique_ptr

I'm writing a game framework, I have a vector> list and I distribute pointers from that list by calling object.get() and sending that out. Before that I send out references instead of raw pointers but that resulted in other weird…
user3680720
  • 51
  • 1
  • 2
  • 5
1
vote
3 answers

Deleting what unique_ptr points to

I have a coding assignment and I need to free whatever memory I allocate, so I'm trying to delete all the semaphores that my unique_ptr's are pointing to. The unique_ptrs are all in a map. The code snippets: static map>…
fool
  • 145
  • 3
  • 11
1
vote
1 answer

Why "const" is not causing compile error

I'm not sure I understand how is this possible: #include #include using namespace std; void f(const unique_ptr &p){ *p = 10; // no error here } int main(){ unique_ptr p (new int); *p = 0; cout << *p <<…
Tracer
  • 2,544
  • 2
  • 23
  • 58
1
vote
2 answers

Guidance Needed: Vectors of unique_ptr to dervied classes from an abstract base class

I'm trying to streamline my code and make it work better and easier: This means diving into vectors and unique_ptr, about which I've read so many good things. However, they are entirely new to me. I have read a few pages on both, but its a lot to…
Nonlin
  • 550
  • 1
  • 6
  • 18
1
vote
3 answers

unique_ptr creation clarification

This is with XCode C++ language dialect c++1y. Help me understand unique_ptr: //1 auto ouput2 = make_unique(AlgorithmFactory::create("YamlOutput", …
Steve M
  • 9,296
  • 11
  • 49
  • 98