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

How do I switch this to unique_ptr?

How do I make it so I dont have to manually delete the pointer? With unique_ptr in the vector<> ? Here is my code: class vec2 { public: double x; double y; vec2() { x = 0.0; y = 0.0; } vec2(double xx, double yy) { x = xx; …
user366866
  • 51
  • 1
  • 4
0
votes
1 answer

Unique ptr with custom allocator and deleter

I'm trying to use a unique ptr in the following scenario: MyClass *pMC = NULL; if ( !MyCustomAlloc(void ** (&pMC), sizeof(MyClass) ) { return false; } // do things if (something else fails) { MyCustomDelete(pMC); return false; } Now,…
0
votes
0 answers

Confusing error in initializer list using std::move on unique_pointers

I'm getting this error from this constructor Severity Code Description Project File Line Suppression State Error (active) E0289 no instance of constructor "std::unique_ptr<_Ty, _Dx>::unique_ptr [with _Ty=InputComponent,…
0
votes
1 answer

G2O BlockSolver Initialization Crash on Unix System

I have a project which possesses and uses G2O library, it runs on both platforms(win/Unix). (WINDOW PASSED / UNIX CRASHED) We can see in both platforms, these lines: g2o::SparseOptimizer optimizer; g2o::BlockSolver_6_3::LinearSolverType *…
0
votes
1 answer

Using SDL_Cursor with unique_ptr : error incomplete type is not allowed

I am trying to create a unique_ptr of type SDL_Cursor, but am unable to do so as the SDL_Cursor definition is within one of SDL's .dll files. The SDL_Cursor struct is declared but not defined in SDL_mouse.h: typedef struct SDL_Cursor SDL_Cursor; So…
0
votes
2 answers

Cannot insert std::unique_ptr with custom deleter with std::move

I use a std::unique_ptr with a custom deleter as a value of a std::map as follows: #include #include #include void deleter(int* p){ std::cout<<"Deleting..."<
yawara
  • 11
  • 2
0
votes
0 answers

Fixing assignment of an object's pointer members via smart pointers

I am learning more about smart pointers in C++14. Consider the following MWC: #include #include #include class House { public: House &operator=(const House &house) = default; House(const House &house) = default; …
0
votes
1 answer

Boost (de)serialize vector of dervied objects, use of deleted finction (unique_ptr)

I am trying to implement serialization into my project. My problem is that when I'm trying to deserialize objects, I'm getting error like: /usr/include/c++/5/ext/new_allocator.h:120:4: error: use of deleted function ‘std::unique_ptr<_Tp,…
Taknie
  • 109
  • 9
0
votes
1 answer

A handle to temporary unique_ptr returned by function

I've tried to read some posts regarding ownership of objects, and how some exceptions apply when copy-constructing a unique_ptr which essentially is forbidden. Of course I pass AND return it by reference, so no copying has to be…
Jerkwaad
  • 1
  • 2
0
votes
0 answers

No appropriate default constructor available for a std::unique_ptr inside of std::unordered_map

This is a continuation of my previous post: (Sorry for the chain of questions) No appropriate default constructor available for std::unique_ptr I encountered a similar error, this time with std::unique_ptr in an std::unordered_map container: (Sorry…
Nicholas Humphrey
  • 1,043
  • 14
  • 29
0
votes
0 answers

How to initiate std::unique_ptr in the constructor with std::make_unique()

Update I read the other post and figured out that I can write: cGraphics() : m_Window(nullptr, SDL_DestroyWindow), m_Renderer(nullptr, SDL_DestroyRenderer) {}, which removes the first error. However it still shows the second error: error C2512:…
Nicholas Humphrey
  • 1,043
  • 14
  • 29
0
votes
1 answer

std::unique_ptr dereference exception not catch-ed in try-catch block

So lets say I have: struct test { bool a; int b; }; int main() { std::unique_ptr ptr; // don't init the ptr try { if (!ptr->a) { std::cout << "ok" << std::endl; } } catch…
code_fodder
  • 12,697
  • 10
  • 68
  • 123
0
votes
1 answer

Unique_ptr in a vector as an attribute of a struct

I am having a little trouble in C++ with a struct that shall contain a unique_ptr. A part of the struct is struct test { std::vector > allFunctions; } where function is another struct. I…
0
votes
0 answers

make_unique how pass complex type as ctor parameter?

Given the code below: MyClass::MyClass(std::tuple param) : mytuple(param){} tuple mt = { 1,2 }; auto myClass = make_unique(mt); // ok auto myClass1 = make_unique({1,2}); // error auto myClass01 =…
Dmitry
  • 99
  • 7
0
votes
1 answer

Filling a vector of unique_ptr with multiple threads?

I have a class that, given a device ID, initializes that device. The destructor of the class spools down the device again. As I have a multiple of these devices connected to my system, I wrote an enumerator class that initializes each connected…
DevSolar
  • 59,831
  • 18
  • 119
  • 197
1 2 3
99
100