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

unique_ptr setup

I have the following situation: if (condition) { std::unique_ptr a(new AClass); // code that breaks various laws of physics } But I need to change it as the pointer could now be one of two types but if I do this: if (condition) { …
Stefan
  • 3,669
  • 2
  • 32
  • 43
1
vote
2 answers

std::unique_ptr ostream inserter

I have enabled unique_ptr in gcc 4.4.6 using the -std=c++0x option. It appears to be working quite well and is exactly what I needed -- a scoped pointer with a custom deleter. However I did notice a problem. typedef std::unique_ptr XPtr; XPtr…
Dale Wilson
  • 9,166
  • 3
  • 34
  • 52
1
vote
1 answer

Does Visual C++ 2010 Beta 1 have unique_ptr, and if not, where can I get a C++0x reference implementation?

I do know: It wasn't in the CTP It's slated to be in the final release I can't find it in Beta 1 I want to play with it
Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
0
votes
1 answer

C++ passing vector of unique_ptrs as parameters to corresponding element of another equal-length vector (especially parallelly)

I have: vector of unique_ptrs of ObjectA vector of newly default constructed vector of ObjectB, and a function in Object B that has signature void f(unique_ptr o). (word Object omitted from here on) How do I do Bvec[i].f(Avec[i]) for all…
chemelnucfin
  • 411
  • 1
  • 4
  • 9
0
votes
1 answer

unique_ptr deleter is causing a crash in my program

At run time when I close my program I get the error: "crt detected that the application wrote to memory after end of heap buffer." I followed the program execution through a destructor to the deleter of the unique ptr and the error occured on the…
Steve
  • 194
  • 11
0
votes
1 answer

How I can fill a unique_ptr that is pointing to a char array?

It's possible to put something in to an unique_ptr that is pointing to a char array, something like this: #include #include #define LEN 100 using std::cout, std::cin; int main() { std::unique_ptr ptr {new…
0
votes
0 answers

Using unique_ptr in a factory method

I'm developing a message system that has to be able to handle different types of messages with different fields and values depending on an ID unique to each type of message. To do so I've created a base class msg_base from which all other messages…
0
votes
1 answer

How to use smart pointer to manage existing object

Suppose I have a vector of data generated from some API. It's for plotting only. vector LineData = generateData(); I also have a class that do the plotting and manage the data class LineChart { public: LineChart(vector*…
qiu
  • 13
  • 4
0
votes
1 answer

How to dereference std::unique_ptr?

The operator* works for std::unique_ptr> but not for std::unique_ptr. But why? Coming from cppreference: These member functions are only provided for unique_ptr for the single objects i.e. the primary…
digito_evo
  • 3,216
  • 2
  • 14
  • 42
0
votes
0 answers

Cannot instantiate the abstract class

Here are my projects files compiled with VS: Init.h #pragma once #include #include #include "Ishare.h" using namespace std; class Init { private: std::unique_ptr p_ifShare; int constData; int…
0
votes
1 answer

Insert the whole vector as element in other vector

I tried to define move constructor Person(Person&&) and delete default one Person() = delete, but I still get std::unique_ptr>::unique_ptr(const std::unique_ptr> &)': attempting…
theateist
  • 13,879
  • 17
  • 69
  • 109
0
votes
0 answers

Bison C++ invalid use of unique_ptr

I am writing a bison file for parsing. In a declared grammar rule, I'm trying to create a unique_ptr pointing to a syntax tree node (DeclAST) that contains multiple symbol table entries. However, an error message was encountered: parser.y:73:19:…
0
votes
2 answers

Polymorphic data types casting with objects wrapped within std::unique_ptr

I have a class hierarchy as follows #include #include class A { public: virtual void Print() = 0; }; class B : public A { public: void Print() { std::cout<<"Hello, world\n"; } }; void…
Aayush Anand
  • 25
  • 1
  • 6
0
votes
0 answers

Why aren't there memory leaks for std::unique_ptr initialized by new since C++17?

Let me quote part of the section in Professional C++ 5th. ed.(Page 237): Before C++17, you had to use make_unique() ... Consider the following call to a function called foo(): foo(unique_ptr { new Simple{} }, unique_ptr { new Bar {…
o_oTurtle
  • 1,091
  • 3
  • 12
0
votes
1 answer

Overriding the deleter of unique_ptr

I have the following code in C++ #include #include class MyType{ public: ~MyType() { std::cout<<"Destructor called"; } }; template struct deleter { deleter(void) {} template
Vivek Mangal
  • 532
  • 1
  • 8
  • 24