unique-ptr

std::move a const std::vector in a lambda capture

独自空忆成欢 提交于 2020-04-30 07:44:07
问题 Motivation: I'm trying to transfer a std::vector<std::unique_ptr<some_type>> to a different thread, via a lambda capture. Since I need the vector to not be cleaned up when the function goes out of scope, I need to take it by value (and not by reference). Since it's a vector of unique_ptrs, I need to move (and not copy) it into the capture. I'm using a generalized lambda capture to move the vector while capturing. Minimal program to illustrate the concept: auto create_vector(){ std::vector<std

Efficiently erase a unique_ptr from an unordered_set

孤人 提交于 2020-04-11 05:47:07
问题 I am storing the ownership of some objects inside an unordered_set , using unique_ptr s. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like this: typedef unique_ptr<MyType> MyPtr; unordered_set<MyPtr> owner; MyPtr p = make_unique<MyType>("foo") MyType *pRaw = p.get(); owner.insert(std::move(p)); // Later ... // I want to do something like this (cannot be written as-is, of course): // owner.erase(pRaw); Is there a way to do this? I can

Efficiently erase a unique_ptr from an unordered_set

时光总嘲笑我的痴心妄想 提交于 2020-04-11 05:44:14
问题 I am storing the ownership of some objects inside an unordered_set , using unique_ptr s. But I don't know a good way to erase one of them from the set, when the time comes. Code looks something like this: typedef unique_ptr<MyType> MyPtr; unordered_set<MyPtr> owner; MyPtr p = make_unique<MyType>("foo") MyType *pRaw = p.get(); owner.insert(std::move(p)); // Later ... // I want to do something like this (cannot be written as-is, of course): // owner.erase(pRaw); Is there a way to do this? I can

Why unique_ptr and shared_ptr do not invalidate the pointer they are constructed from?

一曲冷凌霜 提交于 2020-03-15 05:46:42
问题 A note: this is an API design question , riding on the design of the constructors of unique_ptr and share_ptr for the sake of the question, but not aiming to propose any change to their current specifications. Though it would usually be advisable to use make_unique and make_shared , both unique_ptr and shared_ptr can be constructed from a raw pointer. Both get the pointer by value and copy it. Both allow (i.e. in the sense of: do not prevent ) a continuance usage of the original pointer

In the case of using a std::unique_ptr to automatically deallocate memory upon exiting a scoped block, why not just use the stack?

你。 提交于 2020-03-04 18:44:25
问题 This is a great answer about smart pointers, such as unique pointers: What is a smart pointer and when should I use one?. Here is an example they provide as the simplest use of a unique pointer: void f() { { std::unique_ptr<MyObject> ptr(new MyObject(my_constructor_param)); ptr->DoSomethingUseful(); } // ptr goes out of scope -- // the MyObject is automatically destroyed. // ptr->Oops(); // Compile error: "ptr" not defined // since it is no longer in scope. } However, this begs the question:

shared_ptr<> is to weak_ptr<> as unique_ptr<> is to… what?

◇◆丶佛笑我妖孽 提交于 2020-01-26 23:56:10
问题 In C++11, you can use a shared_ptr<> to establish an ownership relation with an object or variable and weak_ptr<> to safely reference that object in a non-owned way. You can also use unique_ptr<> to establish an ownership relation with an object or variable. But what if other, non-owning objects want to also reference that object? weak_ptr<> isn't helpful in this case. Raw pointers are helpful but bring various downsides (e.g. they can be automatically initialized to nullptr but this is

why std::unique_ptr vector gets invalid pointer exception

强颜欢笑 提交于 2020-01-21 19:16:27
问题 I wrote simple code to help me understand smart pointers: string s = "str"; vector <unique_ptr<string>> pv ; pv.push_back(unique_ptr<string>(&s)); cout<<*(pv[0])<<endl; This code compiles fine, but gets me a runtime error: str * Error in `...': munmap_chunk(): invalid pointer: 0x00007ffd956e57e0 * Aborted (core dumped) What happened and what have I done wrong? 回答1: In the std::unique_ptr 's destructor it will call delete on the &s pointer which was not allocated via new . Just use: std:

why std::unique_ptr vector gets invalid pointer exception

淺唱寂寞╮ 提交于 2020-01-21 19:16:08
问题 I wrote simple code to help me understand smart pointers: string s = "str"; vector <unique_ptr<string>> pv ; pv.push_back(unique_ptr<string>(&s)); cout<<*(pv[0])<<endl; This code compiles fine, but gets me a runtime error: str * Error in `...': munmap_chunk(): invalid pointer: 0x00007ffd956e57e0 * Aborted (core dumped) What happened and what have I done wrong? 回答1: In the std::unique_ptr 's destructor it will call delete on the &s pointer which was not allocated via new . Just use: std:

Why is this not a memory leak in C++?

强颜欢笑 提交于 2020-01-21 12:21:45
问题 A couple of months ago I asked this question where I asked why there was a memory leak. Apparently, I forgot a virtual destructor. Now I'm struggling to understand why this is not a memory leak: #include <iostream> #include <vector> #include <memory> using namespace std; class Base{ public: explicit Base(double a){ a_ = a; } virtual void fun(){ cout << "Base " << a_ << endl; } protected: double a_; }; class Derived : public Base{ public: Derived(double a, double b): Base(a), b_{b}{ } void fun

Copy constructor for a class that has unique ptr of a Base class [duplicate]

若如初见. 提交于 2020-01-15 08:30:13
问题 This question already has answers here : Copy constructor for a class with unique_ptr (6 answers) Closed 5 years ago . When a class has a unique_ptr of a Base class what is a good way to implement the copy constructor. Let me try to explain it with an example: struct Base { virtual void doSth() = 0; // to make the class abstract. }; struct Derived : public Base { virtual void doSth() override {} }; struct Foo { std::unique_ptr<Base> bar; Foo(const Foo& other) : bar(new Base(*other.bar)) //