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

Unique pointer in Linked List - Unhandled exception, stack overflow

First of all, let me say thank you for all the help I've been received in the last couple hours. I've been struggle with this problem, how to convert from raw pointer to unique pointer and got myself into a lot of errors. However, with the help of…
Hoang Minh
  • 1,066
  • 2
  • 21
  • 40
1
vote
2 answers

Using unique_ptr and vector

I have this code: v8::Handle StartMethod(const v8::Arguments &args) { v8::HandleScope scope; // node_isolate int length = args.Length(); std::vector> argv; for(int i=0;i
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
1
vote
1 answer

Moving an array held by a unique_ptr into a vector's array storage

Is there a way to move a unique_ptr unique_ptr foo; into a vector? vector bar;
huitlarc
  • 1,993
  • 2
  • 13
  • 12
1
vote
1 answer

Using C++ inheritance to enhance a class with ownership semantics

I have some (in my opinion) fairly specific ownership requirements: I have a class that basically interprets an array of doubles is a specific way (a concatenation of some fairly large matrices), and want to communicate with a C library that…
1
vote
2 answers

C++ Design: Pool and pointers VS client-server

I'm designing a software tool in which there's an in-memory model, and the API user can get objects of the model, query them and set values. Since all the model's objects belong to a single model, and most operations must be recorded and tested,…
1
vote
2 answers

Smart way to construct class member std::vector >

This question combines unique_ptr as class member and move semantics fail to compile with clang and C++ std::vector in constructor. My goal is to construct a wrapper struct V_wrapper{ std::vector > vec; …
Plamen
  • 650
  • 1
  • 8
  • 27
1
vote
1 answer

unique_ptr as class member and move semantics fail to compile with clang

I can't get clang (Apple LLVM version 4.2 (clang-425.0.28)) to compile these classes: struct A { int f(){return 2;} }; class Cl{ std::unique_ptr ptr; public: Cl(){ptr = std::unique_ptr(new A);} Cl(const Cl& x) : ptr(new…
Plamen
  • 650
  • 1
  • 8
  • 27
1
vote
3 answers

Auto pointer (smart) in vector

I am having trouble in solving the below code. I understand auto_ptr cannot be used in STL due to the copy issue. But I am not able to solve this using the C++11 unique_ptr as well. Can you please help me solve this? Error: $ g++ -std=c++0x…
user1663533
  • 95
  • 2
  • 10
1
vote
2 answers

std::function with unique_ptr argument

Given a function like void MyFunction(std::unique_ptr arg); it is not possible (MSVC 2012) to create a functor like std::function)> f = std::bind(&MyFunction, std::placeholders::_1); The problem is not the bind -…
Zero
  • 11,593
  • 9
  • 52
  • 70
1
vote
2 answers

Passing unique pointer to template function

I'm trying to figure out how exactly the unique_ptr type plays with templates in C++11, and am not having much luck: Specifically, I'm trying to make a template function which inserts a unique_ptr value for a given unique key in a given map if there…
errantlinguist
  • 3,658
  • 4
  • 18
  • 41
1
vote
1 answer

Trying to wrap std containers to store rvalue references (like unique_ptr, but on the stack)

I'm trying to do strange things again. Okay, here's the general idea. I want a std::list (and vector and so on) that actually own the objects they contain. I want to move the values into it, and access them by reference. Example using a list of…
Sod Almighty
  • 1,768
  • 1
  • 16
  • 29
1
vote
1 answer

map of structs using unique ptr : does not build on visual but works on clang

I'm having these two simple codes : void f(){ std::map> map_; std::unique_ptr p; map_[42] = std::move(p); } does build struct test_s{ int toto; std::unique_ptr tata; }; void f(){ …
dzada
  • 5,344
  • 5
  • 29
  • 37
1
vote
1 answer

uniq_ptr direct assignment

I have a function like this: unique_ptr foo() { return unique_ptr(new int[4]) } When calling this foo(), what I do is: unique_ptr t = foo() I am wondering is there any problem in this piece of code? Should I use something like…
WhatABeautifulWorld
  • 3,198
  • 3
  • 22
  • 30
1
vote
3 answers

One struct with unique_ptr fields but with different deleters

Using Visual Studio 2010, I have: using namespace std; struct C { unique_ptr> Field1; unique_ptr> Field2; unique_ptr> FieldN; } It is going to be used in two…
Adam
  • 3,872
  • 6
  • 36
  • 66
1
vote
1 answer

C++11 - Copy construction of a smart pointer pointing to abstract type?

I like std::unique_ptr. It helps me out to prevent memory leaks, which is extremely useful. But there's one problem: copy assignment and construction is not allowed. Even though this restriction serves safety of a programmer, it is quite limiting,…
Helixirr
  • 911
  • 9
  • 18