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:

1871 questions
0
votes
1 answer

assignment operator error in unique pointer

I tried to use unique_ptr in c++ in a singelton pattern instead of raw pointer. when I want to assign a unique_ptr to another I got an error. I tried to use std::move to assign but it did not work. the code is as follow: #include #include…
user10875974
0
votes
1 answer

What is the simplest way to stash a unique_ptr on the heap?

Long time Java, first time C++. I'm writing a Java class that wraps an existing C++ API and looks something like this: public class Foo implements Closeable { private long handle; public Foo(File file) { this.handle =…
Archie
  • 4,680
  • 1
  • 26
  • 35
0
votes
1 answer

How to move an unique_ptr from a parameter pack inside a template method

I want to pass a parameter pack into a template method that creates a new instance of an object of a type specified by another template argument. Minimal code example: my code actually does more operations with the created type and the…
The Shmoo
  • 196
  • 12
0
votes
2 answers

How to forward declare custom unique_ptr for shared memory

I've taken the example below from Dr. Rian Quinn's book "Hands-On System Programming with C/C++" modified just a bit. It wraps mmap with a unique_ptr{}. It works almost just like I need. I would like to forward declare the pointer to the mapped…
Rob
  • 303
  • 3
  • 5
0
votes
0 answers

smart pointer with unexpected behavior

As i was going through smart pointers, I ran through the following code. Works as expected. #include #include using namespace std; class Double { public: Double(double d = 0) : dValue(d) { cout << "constructor: " << dValue…
Newbie
  • 197
  • 2
  • 11
0
votes
1 answer

Is it possible to implement a std::list using unique pointers?

List.h: #pragma once #include #include #include class List{ public: List(); List(const std::initializer_list &list); ~List(); int size() const; void…
0
votes
1 answer

How can i change my const char pointer to unique pointer?

So I am trying to change my game and I found a problem where I couldn't make the change: I have the following code: std::string fileContents = ""; const char* contentsPtr = fileContents.c_str(); I tried making it: const std::unique_ptr
Ninhow
  • 49
  • 7
0
votes
2 answers

Guarantee Unique objects in factory using unique_ptr

I came across the following code for a factory. T::create(std::forward(args)...) returns a pointer to an object created dynamically. So basically if two objects have the same address then they are the same. unique_ptr guarantees that a single…
gringo
  • 311
  • 4
  • 13
0
votes
0 answers

Factory design pattern returning static unique pointer in C++

I had a question. I am working with external library in C++. The factory returns the unique pointer of the object. E.g. there is shapefactory which gives different shape different with different arguments static std::unique_ptr
Dhruvin Naik
  • 79
  • 10
0
votes
2 answers

unique_ptr to nullptr is uncopyable

It looks like creating nested vectors of unique_ptr to null throws attempting to reference a deleted function. I believe this is because it's trying to copy the vector unique_ptr(nullptr)'s and unique_ptr(nullptr) is uncopyable. #include…
TMoore
  • 51
  • 5
0
votes
2 answers

Moving a unique_ptr in the same statement it is used

Is doing something like this safe? I'm unsure if the execution order is guaranteed or not. auto foo = std::make_unique(); foo->Bar(std::move(foo));
0
votes
1 answer

CPP guidelines: Reseating a smart pointer

R.33 is confusing me. Can someone care to explain further? The meaning of "reseating" doesn't seem to work here. ?? R.33: Take a unique_ptr& parameter to express that a function reseats thewidget Reason Using unique_ptr in this way both documents…
0
votes
3 answers

How to return/copy values of an unique_ptr?

I have a simple class with one attribute std::unique_ptr in C++. I want to have a function that converts string to std::unique_ptr, other to convert float to std::unique_ptr, and a third to return…
Felipe
  • 3,986
  • 5
  • 29
  • 62
0
votes
1 answer

unique_ptr/auto_ptr look alike with custom deleter for c++98

auto_ptr doesn't support custom deleter and tr1 shared_ptr is not a good option for me. Are there any good options before c11 for unique_ptr/ auto_ptr look alike with custom deleter?
0
votes
1 answer

Differences when using shared_ptr and unique_ptr to point to an array?

In C++11, I just found that it looks like there are some differences between shared_ptr and unique_ptr when they are used to allocate an array. I would like to get confirmation if what I found is correct. I must use for unique_ptr but
rudky
  • 89
  • 3
1 2 3
99
100