shared_ptr implementation I've written a linked-list version of something like shared_ptr, which gets destroyed when the last copy of a pointer is destroyed.

Aside from the thread-unsafety, is it a fine implementation? Anything I could improve?

Also, what's the best way to extend it to make it work with HANDLEs, etc., without repeating (or limiting) myself unnecessarily, while avoiding excess verbosity (e.g. auto_&lt;HANDLE, custom_deallocator&lt;HANDLE&gt; &gt; is considered too verbose)?

template&lt;typename T&gt;
class auto_
{
    T *pValue;
    mutable const auto_&lt;T&gt; *pPrev, *pNext;

public:
    auto_()           : pValue(new T()),  pPrev(NULL), pNext(NULL) { }
    auto_(T *pValue)  : pValue(pValue),   pPrev(NULL), pNext(NULL) { }
    auto_(const T &amp;v) : pValue(new T(v)), pPrev(NULL), pNext(NULL) { }
    auto_(const auto_&lt;T&gt; &amp;o) : pValue(o.pValue), pPrev(&amp;o), pNext(NULL) { o.pNext = this; }

    virtual ~auto_()
    {
        const auto_&lt;T&gt; *const pPrev = this-&gt;pPrev, *const pNext = this-&gt;pNext;
        if (pPrev != NULL) { pPrev-&gt;pNext = pNext; }
        if (pNext != NULL) { pNext-&gt;pPrev = pPrev; }
        if (pPrev == NULL &amp;&amp; pNext == NULL) { delete this-&gt;pValue; }
        this-&gt;pPrev = this-&gt;pNext = NULL;
        this-&gt;pValue = NULL;
    }

    auto_&lt;T&gt;&amp; operator=(const auto_&lt;T&gt;&amp; other)
    {
        if (this != &amp;other)
        {
            this-&gt;~auto_();
            this-&gt;pValue = other.pValue;
            this-&gt;pPrev = &amp;other;
            this-&gt;pNext = other.pNext;
            if (other.pNext != NULL) { other.pNext-&gt;pPrev = this; }
            other.pNext = this;
        }
        return *this;
    }

    operator   T&amp;() /*also const version*/ { return *this-&gt;pValue; }
    operator   T*() /*also const version*/ { return  this-&gt;pValue; }
    T* operator-&gt;() /*also const version*/ { return  this-&gt;pValue; }
    T&amp; operator *() /*also const version*/ { return *this-&gt;pValue; }
};


Sample usage:

template&lt;typename T&gt;
T recurse(T value, int depth)
{
    if (depth &gt; 0) { T result = recurse(value, depth - 1); return result; }
    else { return value; }
}

auto_&lt;int&gt; test()
{
    printf("Value: %d\n", *recurse(auto_&lt;int&gt;(10), 3));
    auto_&lt;int&gt; p1 = recurse&lt;auto_&lt;int&gt; &gt;(5, 3);
    printf("Value: %d\n", *p1);
    auto_&lt;int&gt; p2 = 3;
    p1 = p2;
    p2 = p1;
    return p2;
}
Implementing your own smart pointer is very hard please don't try.

If it is just to try and learn then fine you may learn something but after practicing go back to one that has been tested and is know to work.

20 seconds into looking Bug 1:

int main()
{
    auto_&lt;int&gt;   x;
    auto_&lt;int&gt;   y(x);
    auto_&lt;int&gt;   z(x);
}


Problem caused by this line:

auto_(const auto_&lt;T&gt; &amp;o)
   : pValue(o.pValue)
   , pPrev(&amp;o)
   , pNext(NULL)
{
    o.pNext = this;   // Here you are overwriting x.pNext when building z
}                     // It may or may not cause a bug but it was definitely
                      // not what you intended to do.


You now have

pNext List
[X] -&gt; [Z] -&gt; |    Y/Z point at nothing
       [Y] -&gt; |    X   points at Z

pPrev List
[Y] -&gt; [X] -&gt; |    Y/Z point at X
[Z] ----^          X   points at nothing.

                   One assumes you are trying to create a circular list!


Don't use hungarian notation

pValue


Do you really want to bind your object to always using pointers?
Just use logical names for the members try not to encode type information into the name of the member it already has that information in its type.

Style Tip

Don;t do this:

    const auto_&lt;T&gt; *const pPrev = this-&gt;pPrev, *const pNext = this-&gt;pNext;


It really hard to read. You are doing complex enough stuff try and make it easy to read

    auto_&lt;T&gt; const* const pPrev = this-&gt;pPrev
    auto_&lt;T&gt; const* const pNext = this-&gt;pNext;


Don't do pointless work

    this-&gt;pPrev = this-&gt;pNext = NULL;
    this-&gt;pValue = NULL;


This does nothing. This object is going to be destroyed. Therefore these variables do not exist after the destructor exists. So little point in playing with their values just before destruction.

Don't manually call the destructor

        this-&gt;~auto_();


Nobody expects people to do this. Its just confusing. Move the code in the destructor into another method and call that.

Broken link

If sombody inserts a NULL pointer into your smart pointer you are asking for trouble when they de-reference it.

auto_&lt;int&gt;  x(NULL); // valid constructor


This is going to blow up

operator   T&amp;() /*also const version*/ { return  *this-&gt;pValue; }


You either need to check the ptr on construction (to make sure it is never NULL) or if you allow NULL pointers then you need to check when the object is used.

Specialization

To avoid repeating yourself allow your code to be specialized by a second template parameter that understands how to reclaim the resources of different types. The default version just calls delete but this allows you to use specialize it for handles etc.

Circular lists are easier with no NULL pointers.

template&lt;typename T&gt;
struct Deleter
{
    void operator()(T* ptr) const { delete ptr;}
};

template&lt;typename T, typename D = Deleter&lt;T&gt; &gt;
class my_auto
{
    T *value;
    mutable const my_auto&lt;T&gt;* prev;
    mutable const my_auto&lt;T&gt;* next;

    public:
    // set up the chain to be circular pointing at just itself.
    // This way next/prev will never be NULL and we don't need to test
    my_auto()           : value(new T()),  prev(this), next(this) { }
    my_auto(T *value)   : value(value),    prev(this), next(this) { if (value == 0) throw int(1);}
    my_auto(const T &amp;v) : value(new T(v)), prev(this), next(this) { }

    // insertInto() will do that appropriate set up.
    // We are currently not in a chain and have no value to release.
    my_auto(const my_auto&lt;T&gt; &amp;o)
    {
        insertInto(o);
    }

    my_auto&lt;T&gt;&amp; operator=(const my_auto&lt;T&gt;&amp; other)
    {
        // Check for assignment to self
        if (this != &amp;other)
        {
            testAndDestroy();
            removeFromChain();
            insertInto(other);
        }
        return *this;
    }

    ~my_auto()
    {
        testAndDestroy();
        removeFromChain();
    }

    private:
        // If next == this then this is the only node in the chain
        // So we destroy the data portion.
        void testAndDestroy()
        {
            if (next == this)
            {
                D     deleter;
                deleter(value);
            }
        }
        // Unlink this node from the chain.
        // If we are the only link in the chain it still works
        // we just remain linked to ourselves.
        void removeFromChain()
        {
            // Remove this node from the chain
            prev-&gt;next  = next;
            next-&gt;prev  = prev;
        }
        // Insert into another chain.
        // Assumes that we are not part of another chain
        // and that our data has already been released as required.
        void insertInto(const my_auto&lt;T&gt;&amp; other)
        {
            value           = other.value;

            next            = other.next;
            other.next.prev = this;

            prev            = other;
            other.next      = this;
        }
};


If you want to use a non circular list (then you need to fix the constructor)

auto_(const auto_&lt;T&gt; &amp;o)
    : pValue(o.pValue)
    , pPrev(&amp;o)
    , pNext(o.pNext)                      // Fix this line
{
    if (o.pNext) { o.pNext.prev = this;}  // Add this line
    o.pNext = this;
}
// Your code assumes the node is always added to the end of the list
// You can not guarantee this. So you need to take account of being
// inserted into the middle.