smart-pointers

Use of observer_ptr

こ雲淡風輕ζ 提交于 2019-11-28 07:10:23
What exactly is the point of the construct std::observer_ptr in the library fundamentals technical specification V2? It seems to me that all it does is wrap a bare T* , which seems like a superfluous step if it adds no dynamic memory safety. In all of my code I use std::unique_ptr where I need to take explicit ownership of an object and std::shared_ptr where I can share ownership of an object. This works very well and prevents accidental dereferencing of an already destroyed object. std::observer_ptr makes no guarantee about the lifetime of the object observed, of course. If it were to be

C++0x unique_ptr replaces scoped_ptr taking ownership?

可紊 提交于 2019-11-28 06:53:57
I used to write code like this: class P {}; class Q: public P {}; class A { // takes ownership A(P* p): p_(p) {} scoped_ptr<P> p_; }; A a(new Q); With C++0x, should I rewrite class A as: class A { // takes ownership A(unique_ptr<P>&& p): p_(p) {} unique_ptr<P> p_; }; I've upvoted comonad's answer, but with a caveat: Whenever you want to explicitely disallow move semantics, use a scoped_ptr const unique_ptr . I have not come across any use cases where a const std::unique_ptr is inferior to a boost::scoped_ptr . However I'm open to education on the subject. Edit: Here is a use case of boost:

How to break shared_ptr cyclic reference using weak_ptr

丶灬走出姿态 提交于 2019-11-28 06:31:48
I have read that weak_pointers can be used to break cyclic references. Consider the following example of a cyclic reference struct A { boost::shared_ptr<A> shrd_ptr; }; boost::shared_ptr<A> ptr_A(boost::make_shared<A>()); boost::shared_ptr<A> ptr_b(boost::make_shared<A>()); ptr_A->shrd_ptr = ptr_b; ptr_b->shrd_ptr = ptr_A; Now above is a case of cyclic reference and I wanted to know how I can break the cyclic reference above by using weak_ptr ? Update : Based on suggestion received I came up with the following : struct A { boost::weak_ptr<A> wk_ptr; }; boost::shared_ptr<A> ptr_A (boost::make

Using shared_ptr for unique ownership (kind of) - is this good practice?

时光总嘲笑我的痴心妄想 提交于 2019-11-28 06:14:51
问题 this is quite hard to explain but I'll try my best. So, I have a RenderComponent, EventManager and RenderSystem. In my RenderComponents constructor, I raise a renderComponentCreated event which the RenderSystem subscribes to. Using an event args object, I pass a RenderNode as data which contains the information the renderSystem needs to draw the thing (drawable, position and type). So far so good. Now, when the renderComponent is deleted, I want the RenderNode to be removed from the

Bad practice to return unique_ptr for raw pointer like ownership semantics?

心不动则不痛 提交于 2019-11-28 06:09:53
I've written a static factory method that returns a new Foobar object populated from another data object. I've recently been obsessed with ownership semantics and am wondering if I'm conveying the right message by having this factory method return a unique_ptr . class Foobar { public: static unique_ptr<Foobar> factory(DataObject data); } My intent is to tell client code that they own the pointer. Without a smart pointer, I would simply return Foobar* . I would like, however, to enforce that this memory be deleted to avoid potential bugs, so unique_ptr seemed like an appropriate solution. If

Create a boost::shared_ptr to an existing variable

五迷三道 提交于 2019-11-28 05:50:09
I have an existing variable, e.g. int a = 3; How can I now create a boost::shared_ptr to a ? For example: boost::shared_ptr< int > a_ptr = &a; // this doesn't work although you should put the variable into a managed pointer on it's creation to do it from an existing pointer. int *a=new int; boost::shared_ptr<int> a_ptr(a); That said you most definitely do not want to be putting stack variables into shared_ptr BAD THINGS WILL HAPPEN If for some reason a function takes shared_ptr and you only have a stack varaible you are better off doing this: int a=9; boost::shared_ptr<int> a_ptr=boost::make

Conditionally instantiate a template at run-time

本秂侑毒 提交于 2019-11-28 04:57:04
问题 I have a template class template <class T> class myClass { public: /* functions */ private: typename T::Indices myIndices; }; Now in my main code I want to instantiate the template class depending on a condition. Like : myFunc( int operation) { switch (operation) { case 0: // Instantiate myClass with <A> auto_ptr < myClass <A> > ptr = new myClass<A> (); case 1: // Instantiate myClass with <B> auto_ptr < myClass <B> > ptr = new myClass<B> (); case 2: // Instantiate myClass with <C> .... } //

How could one implement an observer_ptr?

喜欢而已 提交于 2019-11-28 04:44:28
问题 I'd like to use observer_ptr in my project, but the paper only defines the interface, not the complete implementation. Is there an easy way to implement it myself? 回答1: You can create the so called observer_ptr trivially by creating a unique_ptr with a NOP deleter. template<typename T> struct nop_deleter { void operator()(T*) const {} }; template<typename T> using observer_ptr = unique_ptr<T, nop_deleter>; This will still have unique_ptr 's behavior, meaning it's move-only, while you'd want

Smart pointers/safe memory management for C?

柔情痞子 提交于 2019-11-28 04:19:49
I, and I think many others, have had great success using smart pointers to wrap up unsafe memory operations in C++, using things like RAII, et cetera. However, wrapping memory management is easier to implement when you have destructors, classes, operator overloading, et cetera. For someone writing in raw C99, where could you point (no pun intended) to help with safe memory management? Thanks. The question is a bit old, but I figured I would take the time to link to my smart pointer library for GNU compilers (GCC, Clang, ICC, MinGW, ...). This implementation relies on the cleanup variable

How to return smart pointers (shared_ptr), by reference or by value?

放肆的年华 提交于 2019-11-28 02:52:20
Let's say I have a class with a method that returns a shared_ptr . What are the possible benefits and drawbacks of returning it by reference or by value? Two possible clues: Early object destruction. If I return the shared_ptr by (const) reference, the reference counter is not incremented, so I incur the risk of having the object deleted when it goes out of scope in another context (e.g. another thread). Is this correct? What if the environment is single-threaded, can this situation happen as well? Cost. Pass-by-value is certainly not free. Is it worth avoiding it whenever possible? Thanks