weak-ptr

Why can't a weak_ptr be constructed from a unique_ptr?

寵の児 提交于 2019-12-03 04:42:43
问题 If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of which is managed by someone else. So I don't really see why a weak_ptr can't be constructed from a unique_ptr , but only a shared_ptr . Can someone briefly explain this? 回答1: std::weak_ptr can't be used unless you convert it to std::shared_ptr by the means of lock() . if the standard allowed what you

Is calling map::count with an empty weak_ptr as argument safe?

老子叫甜甜 提交于 2019-12-02 17:28:29
问题 Is it safe to call map::count on an uninitialized thus empty weak_ptr safe? I'm still very inexperienced with c++ and do not have the skills to determine this. In my application, a weak_ptr is being held as key in a map and must be found first by the values. If it cannot be found, an uninitialized weak_ptr is return ed and used in map::count . Code Setup map<my_ptr, connection_data, owner_less<my_ptr>> m_connections; typedef map<my_ptr, connection_data, owner_less<my_ptr>>::iterator it; Find

Assigning shared_ptr to weak_ptr

℡╲_俬逩灬. 提交于 2019-12-02 12:49:45
I want to assign constructed shared_ptr to weak_ptr: std::weak_ptr<void> rw = std::shared_ptr<void>(operator new(60), [](void *pi) { operator delete(pi); }); But, when I do rw.expired() , it shows expired means it is empty. Any suggestions where I am going wrong? Thanks in advance. Slava Purpose of std::shared_ptr is to release managed object when last shared pointer which points to it is destroyed or reassigned to somewhere else. You created a temporary shared ptr, assgned it to std::weak_ptr and then it is just destroyed at the end of the expression. You need to keep it alive: auto shared =

Is calling map::count with an empty weak_ptr as argument safe?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 08:22:31
Is it safe to call map::count on an uninitialized thus empty weak_ptr safe? I'm still very inexperienced with c++ and do not have the skills to determine this. In my application, a weak_ptr is being held as key in a map and must be found first by the values. If it cannot be found, an uninitialized weak_ptr is return ed and used in map::count . Code Setup map<my_ptr, connection_data, owner_less<my_ptr>> m_connections; typedef map<my_ptr, connection_data, owner_less<my_ptr>>::iterator it; Find by data my_ptr get_my_ptr_from_data(string data){ my_ptr my_ptr_to_send; for(it iterator = my_ptrs

How to get rid of weak_ptrs in a container

旧城冷巷雨未停 提交于 2019-12-01 18:07:10
I have a class that stores weak_ptrs in a container and later does something if the weak_ptr is not expired: class Example { public: void fill(std::shared_ptr<int> thing) { member.push_back(thing); } void dosomething() const { for (const auto& i : member) if (!i.expired()) ;// do something. the weak_ptr will not be locked } private: std::vector<std::weak_ptr<int>> member; }; If Example is an object that lives forever and fill is used regularily, the vector allocates memory for elements continously, but they are never removed after they expired. Is there any automatic C++ way to get rid of the

C++: Replace raw pointers with shared and weak ptr

烈酒焚心 提交于 2019-12-01 07:03:26
I'm facing a design issue in my program. I have to manage Nodes object which are part of a root ChainDescriptor. Basically it looks like the following: class ChainDescriptor { public: ~ChainDescriptor() { //delete the nodes in nodes... } void addNode(Node *); Node * getNode(); const std::list<Node *>& getNodes() const; std::list<Node *> m_nodes; }; class Node { public: Node(Node *parent); void addChild(Node *node); Node * getChild(const std::string& nodeName); private: Node * m_parent; std::list<Node*> m_childs; }; The ChainDescriptor class owns all the nodes and is responsible of deleting

How to make a c++11 std::unordered_set of std::weak_ptr

回眸只為那壹抹淺笑 提交于 2019-12-01 03:39:35
I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName; It works fine. But I would like to change it to an unordered set. However, I get about six pages of errors when I do that. Any ideas how to do that? After looking through all the pages of error messages I found to lines that might help. /usr/include/c++/4.7/bits/functional_hash.h:60:7: error: static assertion failed: std::hash is not specialized for this type /usr/include/c++/4.7/bits/stl_function.h: In instantiation of ‘bool std::equal_to<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::weak_ptr

What's the performance penalty of weak_ptr?

无人久伴 提交于 2019-12-01 02:07:15
I'm currently designing a object structure for a game, and the most natural organization in my case became a tree. Being a great fan of smart pointers I use shared_ptr 's exclusively. However, in this case, the children in the tree will need access to it's parent (example -- beings on map need to be able to access map data -- ergo the data of their parents. The direction of owning is of course that a map owns it's beings, so holds shared pointers to them. To access the map data from within a being we however need a pointer to the parent -- the smart pointer way is to use a reference, ergo a

Weak Self in Blocks

耗尽温柔 提交于 2019-12-01 01:03:45
Do I need to check if weak self is nil in blocks? I create weakSelf pointer like: __weak typeof(self) weakSelf = self; and in the beginning of the blocks I do if(!weakSelf){return;} is this unnecessary? or does it depend on whether I coded the rest correctly so that when the self dies, others die too? That check is unnecessary, and is giving you a false sense of security. Here's the problem: __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ if (!weakSelf) { return; } // THE LINE OF INTEREST [weakSelf doSomething]; }); At THE LINE OF INTEREST , some other thread

Weak Self in Blocks

时光怂恿深爱的人放手 提交于 2019-11-30 19:12:56
问题 Do I need to check if weak self is nil in blocks? I create weakSelf pointer like: __weak typeof(self) weakSelf = self; and in the beginning of the blocks I do if(!weakSelf){return;} is this unnecessary? or does it depend on whether I coded the rest correctly so that when the self dies, others die too? 回答1: That check is unnecessary, and is giving you a false sense of security. Here's the problem: __weak typeof(self) weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ if (!weakSelf)