Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing auto_ptr in VC++ 8

std::auto_ptr is broken in VC++ 8 (which is what we use at work). My main gripe with it is that it allows auto_ptr<T> x = new T();, which of course leads to horrible crashes, while being simple to do by mistake.

From an answer to another question here on stackoverflow:

Note that the implementation of std::auto_ptr in Visual Studio 2005 is horribly broken. http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98871 http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=101842

I want to use

  • boost::scoped_ptr, for pointers that shouldn't pass ownership.
  • boost::shared_ptr, for pointers in containers and elsewhere where they are required.
  • std::auto_ptr, for pointers that should/can pass ownership.

But since std::auto_ptr is broken for me, I wonder what would be the best approach:

  • Replace std::auto_ptr with something from the net. Like this this one from Rani Sharoni (haven't tried it yet).
  • Use boost::shared_ptr instead. Will of course work, although there will be some minor overhead that I don't care about. But I want to use auto_ptr to signal the intent of the pointer. (See this answer for a vote on this approach.)
  • I will never need to pass ownership in practice, so I shouldn't worry about this.

Update: Here is what I did: I copied the aforementioned auto_ptr implementation by Rani Sharoni. From here.

Did some minor tests:

class T
{
public:
    T() {
        OutputDebugStringA("T\n");
    };
    ~T() {
        OutputDebugStringA("~T\n");
    };
};

{
    fix::auto_ptr<T> x(new T); // This just works.
}
{
    fix::auto_ptr<T> x = (new T); // Doesn't compile. Great!
}
{
    fix::auto_ptr<T> x = fix::auto_ptr<T>(new T); // Transfer of ownership works also.
}

Of course these tests are by no means exhaustive and you shouldn't trust them. Implementing an exception safe templated class is hairy business. At least this works better than the built in one.

Note: I don't know if I'm allowed to use this implementation yet, with respect to copyright. I have emailed Rani and I'm waiting for a reply. I'll update this post when I know more. Permission is granted for everyone to use Rani Sharoni's auto_ptr implementation as you wish.

Thank you for all your replies.

like image 771
Daniel Sönnerstedt Avatar asked Nov 06 '08 21:11

Daniel Sönnerstedt


4 Answers

Move to boost smart pointers.

In the meantime, you may want to extract a working auto_ptr implementation from an old / another STL, so you have working code.

I believe that auto_ptr semantics are fundamentally broken - it saves typing, but the interface actually is not simpler: you still have to track which instance is the current owner and make sure the owner leaves last.

unique-ptr "fixes" that, by making release not only give up ownership, but also setting the RHS to null. It is the closest replacement for auto-ptr, but with its different semantics it is not a drop-in replacement.

There's an introductory article to boost smart pointers, by, ahem, me.

like image 182
peterchen Avatar answered Sep 27 '22 22:09

peterchen


Have you considered using STLPort?

like image 35
OJ. Avatar answered Sep 27 '22 21:09

OJ.


Use a unique_ptr. I think these were introduced to be a better auto_ptr.

http://www.boost.org/doc/libs/1_35_0/doc/html/interprocess/interprocess_smart_ptr.html#interprocess.interprocess_smart_ptr.unique_ptr

In fact, I'm led to believe auto_ptr may be deprecated in favour of it:

http://objectmix.com/c/113487-std-auto_ptr-deprecated.html

like image 30
Scott Langham Avatar answered Sep 27 '22 22:09

Scott Langham


Why do you think std::auto_ptr<> is broken.

I would have though that somthing as bad as that would have been reported to the standards comitte!

Do you mean that you need to:

std::auto_ptr<T>   x(new T);  // Use the explicit constructor.
like image 44
Martin York Avatar answered Sep 27 '22 23:09

Martin York