Roland said:
Why do by hand what the standard library does for you? It's more
fragile and error prone. (Sure, auto_ptr has some pitfalls, but
Boost/TR1's complementary points improve the situation considerably.)
RAII is a powerful and useful technique. Why ignore it?
In Java yes, but not in C++.
Are you kidding me? Factory functions are a common design pattern. See
guru Sutter's example under "sources and sinks" here:
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
And see the virtual constructor idiom in the FAQ:
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8
You may not personally take advantage of them, but they're out there in
plenty of solid code. (And rightly so.)
The purpose of auto_ptr is that the client is _not_ "required to
clean-up the memory".
While you're right that auto_ptr will automatically delete its pointee
when it goes out of scope either by a normal change of scope or by an
exception (thus freeing the user from remembering/being able to do it),
I was here talking about using auto_ptr primarily to *transfer
ownership* from a factory function that creates an object to a client
who uses it and is responsible for deleting it (i.e., the factory does
not intend to clean it up). The client can delete it either by letting
an auto_ptr handle it, by taking over management by explicitly
releasing the object from the auto_ptr, or by transferring ownership
again to some other object (e.g., another smart pointer) that is
responsible for deleting it (either explicitly or implicitly via RAII).
Apart from that I agree with Howard's position. There are two secrets
of resource management:
1. don't return a resource (not even as "smart" pointer);
That's your rule, not generally accepted C++ wisdom.
2. release resources in the destructor (if not done before) of a
containing object
That's RAII, and it's the same technique auto_ptr uses.
(and allocate the resources in a member function of that object).
That's unnecessary. See "sinks" in the Sutter article mentioned above.
Cheers! --M