unique-ptr

unique_ptr member, private copy constructor versus move constructor

感情迁移 提交于 2019-12-22 09:12:26
问题 Given a base class for multiple derived class, the goal was to create a wrapper class that allowed an STL container to see objects with the base interface, event though different derived classes may actually be added to the container. (See Retrieve data from heterogeneous std::list). After some tinkering, I came up with a new derived class that was a wrapper around a unique_ptr to the base class. However, the move constructor has me confused. class Base { friend class BaseWrapper; virtual

Move `unique_ptr`s between sets

半城伤御伤魂 提交于 2019-12-22 04:42:12
问题 I have two sets and an iterator to an element of a : set<unique_ptr<X>> a, b; set<unique_ptr<X>>::iterator iter = find something in a; I would like to remove the element pointed by iter from a and insert it into b . Is it possible? How? 回答1: Well, I suspect there is no normal way to do it. But there is always a non-normal one :) You can do the following: auto tmp = const_cast<std::unique_ptr<std::string>&&>(*iter); a.erase(iter); b.insert(std::move(tmp)); Ok, the very first line violated set

Move `unique_ptr`s between sets

大城市里の小女人 提交于 2019-12-22 04:42:04
问题 I have two sets and an iterator to an element of a : set<unique_ptr<X>> a, b; set<unique_ptr<X>>::iterator iter = find something in a; I would like to remove the element pointed by iter from a and insert it into b . Is it possible? How? 回答1: Well, I suspect there is no normal way to do it. But there is always a non-normal one :) You can do the following: auto tmp = const_cast<std::unique_ptr<std::string>&&>(*iter); a.erase(iter); b.insert(std::move(tmp)); Ok, the very first line violated set

What's wrong with this initialization of unique_ptr?

丶灬走出姿态 提交于 2019-12-21 13:09:11
问题 Can somebody tell me, what is wrong with the following initialization of unique_ptr? int main() { unique_ptr<int> py(nullptr); py = new int; .... } g++ -O2 xxx.cc -lm -o xxx -std=c++11 says: error: no match for ‘operator=’ (operand types are ‘std::unique_ptr<int>’ and ‘int*’) py = new int; ^ Doing unique_ptr<int> px(new int); works just fine. 回答1: The initialization is fine in both pieces of code, unique_ptr has constructors for both nullptr and naked pointers. What is failing in the first

c++ std::unique_ptr won't compile in map

廉价感情. 提交于 2019-12-21 12:36:48
问题 I'm currently trying to store a std::unique_ptr in a std::unordered_map, but I get a weird compile error. Relevant code: #pragma once #include "Entity.h" #include <map> #include <memory> class EntityManager { private: typedef std::unique_ptr<Entity> EntityPtr; typedef std::map<int, EntityPtr> EntityMap; EntityMap map; public: /* Adds an Entity */ void addEntity(EntityPtr); /* Removes an Entity by its ID */ void removeEntity(int id) { map.erase(id); } Entity& getById(int id) { return *map[id];

How does returning std::make_unique<SubClass> work?

北战南征 提交于 2019-12-21 08:22:10
问题 I have a base class and its subclass: class Base { public: virtual void hi() { cout << "hi" << endl; } }; class Derived : public Base { public: void hi() override { cout << "derived hi" << endl; } }; Trying to create a helper function that creates a unique pointer of a Derived object. 1) This one works: std::unique_ptr<Base> GetDerived() { return std::make_unique<Derived>(); } 2) But, this one fails to compile: std::unique_ptr<Base> GetDerived2() { auto a = std::make_unique<Derived>(); return

Should `unique_ptr< T const [] >` accept a `T*` constructor argument?

徘徊边缘 提交于 2019-12-21 07:43:17
问题 Code: #include <memory> using namespace std; struct T {}; T* foo() { return new T; } T const* bar() { return foo(); } int main() { unique_ptr< T const > p1( bar() ); // OK unique_ptr< T const [] > a1( bar() ); // OK unique_ptr< T const > p2( foo() ); // OK unique_ptr< T const [] > a2( foo() ); // ? this is line #15 } Example errors with Visual C++ 10.0 and MinGW g++ 4.4.1: [d:\dev\test] > cl foo.cpp foo.cpp foo.cpp(15) : error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private

Should `unique_ptr< T const [] >` accept a `T*` constructor argument?

余生颓废 提交于 2019-12-21 07:41:03
问题 Code: #include <memory> using namespace std; struct T {}; T* foo() { return new T; } T const* bar() { return foo(); } int main() { unique_ptr< T const > p1( bar() ); // OK unique_ptr< T const [] > a1( bar() ); // OK unique_ptr< T const > p2( foo() ); // OK unique_ptr< T const [] > a2( foo() ); // ? this is line #15 } Example errors with Visual C++ 10.0 and MinGW g++ 4.4.1: [d:\dev\test] > cl foo.cpp foo.cpp foo.cpp(15) : error C2248: 'std::unique_ptr<_Ty>::unique_ptr' : cannot access private

Am I using the pointer class properly in this generic unique_ptr<>() deleter?

烈酒焚心 提交于 2019-12-21 05:35:08
问题 I created a generic deleter template that can be used to create unique_ptr<>() sub-types allowing for a Deleter other than just delete ptr . It works great with the default optimization flags (i.e. -O0 ), however, when I use -O3 the T & operator * () function, somehow, returns 0 instead of the f_pointer contents. I would like to make sure that we agree that there is something wrong in the compiler and that my template is correct. The following is a complete piece of code that should compile

Is this the right way to implement pimpl wth unique_ptr and move-semantics in C++11

时光怂恿深爱的人放手 提交于 2019-12-21 04:04:34
问题 I haven't yet seen a pimpl example that utilizes both unique_ptr and move-semantics. I want to add a CHelper class to STL derived containers and use pimpl to hide what CHelper does. Does this look right? Derived.h class CDerived : public set<CSomeSharedPtr>, public CHelper { //... }; ` Helper.h // derived containers need to support both copy and move, so CHelper does too class CHelper { private: class impl; unique_ptr<impl> pimpl; public: //--- default: need both cotr & cotr (complete class)