What is boost's shared_ptr(shared_ptr const & r, T * p) used for?

后端 未结 6 1223
孤街浪徒
孤街浪徒 2021-02-01 04:28

boost::shared_ptr has an unusual constructor

template shared_ptr(shared_ptr const & r, T * p);

and I a

相关标签:
6条回答
  • 2021-02-01 04:39

    You might have a pointer to some driver or a lower level api's data structure that may allocate additional data by its lower level api or other means. In this case it might be interesting to increase the use_count but return the additional data if the first pointer owns the other data pointers.

    0 讨论(0)
  • 2021-02-01 04:42

    You can also use this to keep dynamic casted pointers, i.e.:

    class A {};
    class B: public A {};
    
    shared_ptr<A> a(new B);
    shared_ptr<B> b(a, dynamic_cast<B*>(a.get()));
    
    0 讨论(0)
  • 2021-02-01 04:51

    It is useful when you want to share a class member and an instance of the class is already a shared_ptr, like the following:

    struct A
    {
      int *B; // managed inside A
    };
    
    shared_ptr<A>   a( new A );
    shared_ptr<int> b( a, a->B );
    

    they share the use count and stuff. It is optimization for memory usage.

    0 讨论(0)
  • 2021-02-01 04:57

    For "shared_ptr<B> b(a, dynamic_cast<B*>(a.get()));"

    I think it is not the recommended way using smart pointer.

    The recommended way of doing this type conversion should be:

    shared_ptr<B> b(a);
    

    Since in Boost document it is mentioned that:

    shared_ptr<T> can be implicitly converted to shared_ptr<U> whenever T* can be implicitly converted to U*. In particular, shared_ptr<T> is implicitly convertible to shared_ptr<T> const, to shared_ptr<U> where U is an accessible base of T, and to shared_ptr<void>.

    In addition to that, we also have dynamic_pointer_cast which could directly do conversion on Smart Pointer object and both of these two methods would be much safer than the manually casting raw pointer way.

    0 讨论(0)
  • 2021-02-01 05:02

    To expand on leiz's and piotr's answers, this description of shared_ptr<> 'aliasing' is from a WG21 paper, "Improving shared_ptr for C++0x, Revision 2":

    III. Aliasing Support

    Advanced users often require the ability to create a shared_ptr instance p that shares ownership with another (master) shared_ptr q but points to an object that is not a base of *q. *p may be a member or an element of *q, for example. This section proposes an additional constructor that can be used for this purpose.

    An interesting side effect of this increase of expressive power is that now the *_pointer_cast functions can be implemented in user code. The make_shared factory function presented later in this document can also be implemented using only the public interface of shared_ptr via the aliasing constructor.

    Impact:

    This feature extends the interface of shared_ptr in a backward-compatible way that increases its expressive power and is therefore strongly recommended to be added to the C++0x standard. It introduces no source- and binary compatibility issues.

    Proposed text:

    Add to shared_ptr [util.smartptr.shared] the following constructor:

    template<class Y> shared_ptr( shared_ptr<Y> const & r, T * p );
    

    Add the following to [util.smartptr.shared.const]:

    template<class Y> shared_ptr( shared_ptr<Y> const & r, T * p );
    

    Effects: Constructs a shared_ptr instance that stores p and shares ownership with r.

    Postconditions: get() == p && use_count() == r.use_count().

    Throws: nothing.

    [Note: To avoid the possibility of a dangling pointer, the user of this constructor must ensure that p remains valid at least until the ownership group of r is destroyed. --end note.]

    [Note: This constructor allows creation of an empty shared_ptr instance with a non-NULL stored pointer. --end note.]

    0 讨论(0)
  • 2021-02-01 05:03

    I have put shared_ptr's aliasing constructor in use in my little library:

    http://code.google.com/p/infectorpp/ (just my simple IoC container)

    The point is that since I needed a shared_ptr of known type to be returned from a polymorphic class (that does not know the type). I was not able to implicitly convert the shared_ptr to the type I needed.

    In the file "InfectorHelpers.hpp" (line 72-99) you can see that in action for the type IAnyShared.

    Aliasing constructor creates shared_ptr that does not delete the pointers they are actually pointing to, but they still increase the reference counter to the original object and that can be tremendously usefull.

    Basically you can create a pointer to anything using aliasing constructor and threat it as a reference counter.

    //my class
    std::shared_ptr<T> ist;
    int a; //dummy variable. I need its adress
    
    virtual std::shared_ptr<int> getReferenceCounter(){
        return std::shared_ptr<int>(ist,&a); //not intended for dereferencing
    }
    
    virtual void* getPtr(); //return raw pointer to T
    

    now we have both "a reference counter" and a pointer to a istance of T, enough data to create something with the aliasing constructor

    std::shared_ptr<T> aPtr( any->getReferenceCounter(), //share same ref counter 
                   static_cast<T*>(any->getPtr()) ); //potentially unsafe cast!
    

    I don't pretend to have invented this use for the aliasing constructor, but I never seen someone else doing the same. If you are guessing if that dirty code works the answer is yes.

    0 讨论(0)
提交回复
热议问题