Boost logo

Boost :

From: Chris Just (cpjust_at_[hidden])
Date: 2005-02-13 17:39:00


Jonathan Turkanis wrote:
>> I wanted to create a very simple AutoPtr that was essentially the same
>> as the STL auto_ptr (and backwards compatible with it).
>
> Unfortunately, you haven't (see below). Furthermore, auto_ptr is widely
> recognized as unsafe because it is too easy to transfer ownership
> unintentionally.

Of course you can unintentionally transfer ownership if you don't know what you're doing. ;)
I believe that's why you're not supposed to pass an auto_ptr to a function -- instead you pass the
pointer itself with .get(). You can't stop everyone from shooting themselves in the foot.

>>> I only looked very briefly, but I don't see how your auto_ptr can be
>>> returned from functions. Also, why do you need the m_Owner variable?
>>
>> Correct me if I'm wrong, but wasn't there a specific reason why the
>> STL auto_ptr was made so it couldn't be passed to or returned from a
>> function?
>
> This was true of the initial version. The standard version, which is version 3,
> implements a flavor of move semantics (and it doesn't quite work).

I'm using the version that comes with Visual C++ 6.0, whatever version that is.

>> Anyways, if you want to pass or return the pointer to/from a
>> function, just use the .get() or .release() functions.
>
> But this subverts the resource management system.

How? If you pass a pointer to a function, you shouldn't be deleting it inside that function
anyways (so pass it with .get()), and if you return a pointer from a function, you can save it in
a new AutoPtr so it will get deleted automatically...

>> I need the m_Owner variable to tell me if I own the pointer before I
>> delete it.
>
> For auto_ptr, checking that the stored pointer is non-null should be sufficient.

I don't set my pointer to NULL when I release() it, I just set m_Owner = false.

I've also run some speed comparisons between auto_ptr and AutoPtr and (on Windows at least) they
appear to be virtually the same speed when doing the same operations.
I'm wondering what the speed comparisons would be between auto_ptr and some of the other smart
pointers that you mentioned?

   Chris Just

        
                
__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions.yahoo.com/new_mail


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk