We've pretty much moved over to using boost::shared_ptr
in all of our code, however we still have some isolated cases where we use std::auto_ptr
, including singleton classes:
template < typename TYPE >
class SharedSingleton
{
public:
static TYPE& Instance()
{
if (_ptrInstance.get() == NULL)
_ptrInstance.reset(new TYPE);
return *_ptrInstance;
}
protected:
SharedSingleton() {};
private:
static std::auto_ptr < TYPE > _ptrInstance;
};
I've been told that there's a very good reason why this hasn't been made a shared_ptr
, but for the life of me I can't understand why? I know that auto_ptr
will eventually get marked as depreciated in the next standard, so I'd like to know what/how I can replace this implementation.
Also, are there any other reasons why you'd consider using an auto_ptr
instead of a shared_ptr
? And do you see any problems moving to shared_ptr in the future?
Edit:
- So in answer to "can I safely replace
auto_ptr
withshared_ptr
in the above code", the answer is yes - however I'll take a small performance hit. - When
auto_ptr
is eventually marked as depreciated and we move over tostd::shared_ptr
, we'll need to thoroughly test our code to make sure we're abiding by the different ownership semantics.