unique-ptr

To support move semantics, should function parameters be taken by unique_ptr, by value, or by rvalue?

梦想的初衷 提交于 2020-01-09 16:52:08
问题 One of my function takes a vector as a parameter and stores it as a member variable. I am using const reference to a vector as described below. class Test { public: void someFunction(const std::vector<string>& items) { m_items = items; } private: std::vector<string> m_items; }; However, sometimes items contains a large number of strings, so I'd like to add a function (or replace the function with a new one) that supports move semantics. I am thinking of several approaches, but I'm not sure

Unique_ptr destructor saying object has never been allocated

我的梦境 提交于 2020-01-07 09:37:30
问题 I have a main process that owns a Model object. The purpose of this is to share a Model between various objects that performs tasks and modifies the same Model. Right now, I create a unique_ptr to this object and pass a reference to that unique_ptr to other objects to perform certain tasks. The tasks all complete correctly, but something weird happens when it tries to call the destructor on the unique_ptr. It shows: RAW: memory allocation bug: object at 0xd0ebdfb2b8 has never been allocated

Prevent moving of a unique_ptr C++11

女生的网名这么多〃 提交于 2020-01-04 13:42:21
问题 Is there any way to prevent a user to explicity take ownership of a unique pointer with std::move ? 回答1: Make it const The unique_ptr move constructor takes a non-const rvalue reference, so can't be called with a const object. const unique_ptr<int> owner(new int); // ... unique_ptr<int> thief = std::move(owner); // ERROR This allows unique_ptr to be used like a boost::scoped_ptr 回答2: By returning a std::unique_ptr , you have given up the control of the object. The new owner will either

When does std::unique_ptr<A> need a special deleter if A has a destructor?

限于喜欢 提交于 2020-01-03 09:04:15
问题 If the class A in unique_ptr<A> it's own destructor, is it necessary to declare a deleter to ensure that the unique pointer uses that destructor? The example I am thinking of is that A has a member mx of type user_matrix (a name I just made up) which needs to call a function free(...) to release its memory, one would define ~A(){ user_matrix::free(mx); /*etc*/} Since default_deleter<> will call delete , it is my understanding that that should use ~A() . However, the example with the opening

unique_ptr in Qt project

≡放荡痞女 提交于 2020-01-03 08:55:07
问题 I have a simple Qt project. I include <memory> but std::unique_ptr is not available. I know that I should use Qt specific smart pointers but I need to include a larger project that contains std::unique_ptr. What can I do? Thanks! 回答1: C++11 is required for smart pointers. Depending on your version of Qt: Add CONFIG += c++11 to your .pro file if you have Qt5 and above. It needs to include <memory> as Simon mentioned. If you have an earlier version than Qt5, try adding this: QMAKE_CXXFLAGS +=

How to make a class with a member of unique_ptr work with std::move and std::swap?

走远了吗. 提交于 2020-01-02 04:06:07
问题 I have a class. It has a member of unique_ptr. class A { std::unique_ptr<int> m; }; I hope it works with the following statements A a; A b; a = std::move(b); std::swap(a, b); How to make it? According to the comments, I have a question. Is this compiler dependent? If I do nothing, it cannot pass compilation in VC++ 2012. I tried before struct A { A() {} A(A&& a) { mb = a.mb; ma = std::move(a.ma); } A& operator = (A&& a) { mb = a.mb; ma = std::move(a.ma); return *this; } unique_ptr<int> ma;

How to assign the address of an existing object to a smart pointer?

霸气de小男生 提交于 2020-01-01 23:58:07
问题 #include <memory> class bar{}; void foo(bar &object){ std::unique_ptr<bar> pointer = &object; } I want to assign an address of the object to the pointer. The above code obviously wont compile, because the right side of the assignment operator needs to be a std::unique_ptr. I've already tried this: pointer = std::make_unique<bar>(object) But it throws many errors during compilation. How can I do that? Update As said in the answers - using the std::unique_ptr::reset method led to undefined

Why am I allowed to copy unique_ptr? [duplicate]

試著忘記壹切 提交于 2020-01-01 01:52:12
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Returning unique_ptr from functions 20.7.1.2 [unique.ptr.single] defines copy constructor like this : // disable copy from lvalue unique_ptr(const unique_ptr&) = delete; unique_ptr& operator=(const unique_ptr&) = delete; So, why the following code compiles fine? #include <memory> #include <iostream> std::unique_ptr< int > bar() { std::unique_ptr< int > p( new int(4)); return p; } int main() { auto p = bar(); std

boost thread_group move ownership of unique_ptr to thread

混江龙づ霸主 提交于 2019-12-31 07:06:46
问题 What workaround exists to make this code run? The code results in "Attempting to reference a deleted function". unique_ptr is assigned in a loop and then passed on to thread and later got rid of. boost::thread_group threads; std::unique_ptr<ScenarioResult> scenario_result; while ((scenario_result = scenarioStock.getNextScenario()) != nullptr) { threads.create_thread(boost::bind(&Simulation::RunSimulation, boost::ref(grid_sim), std::move(scenario_result))); } 回答1: You can use a lambda

C++ how to pass unique_ptr to function and still benefit from polymorphism?

旧城冷巷雨未停 提交于 2019-12-31 05:37:55
问题 I have the following class definitions: class A {...} class B : public class A {...} and an owner class definition: class C { A* a; public: C (A* a) { this->a = a; } } Then When I try to call the following, everything is fine: B* b = new B(); C(b); But then I have the following owner class definition: class C { std::unique_ptr<A> a; public: C(std::unique_ptr<A>& a) { this->a = std::move(a); } } And when I try to call the following, I get a compiler error saying cannot convert from unique_ptr