convert shared_ptr to auto_ptr?
I need to obtain auto_ptr from shared_ptr in my code. I can do reverse operation - convert auto_ptr to shared_ptr as shared_ptr has such constructor:
template<class Y> explicit shared_ptr(std::auto_ptr<Y> & r);
Can I convert shared_ptr to auto_ptr? Or it is imp开发者_StackOverflowossible by design?
It is impossible by design as the object may be shared with other shared pointer and thus "fetching" it to auto_ptr may lead to deleting referenced object.
For same reason shared_ptr has no "release" member function as auto_ptr.
Edit:
Even if shared_ptr had some kind of "release" method or allowed to remove its reference without destroying the object it would not work for following case (threads A, B):
A: {
A: int count = sp.use_count();
Context Switch
B: shared_ptr<bar> my_sp = weak_sp.lock();
B: // now use_count = 2 but A thinks it is 1
Context Switch
A: auto_ptr<bar> ap;
A: if(count == 1)
A: ap.reset(sp.release());
A: // actutally there is no sp.release but what if
A: ap->foo();
A: } // delete the object pointer by ap as it goes out of scope
Context Switch
B: my_sp->foo(); // Ooops - object is deleted!
A shared pointer can be shared by many things, you can't just take it from them all somehow. This is elaborated by Artyom and peoro.
One approach is to make a temporary auto_ptr
, and release it from handling the pointer at the end of the scope. dalle outlines a first approach, but this suffers from lack of exception-safety (might accidentally delete), and it cannot protect you from accidentally passing it to a function that's going to transfer ownership (where the delete falls out of our hands).
We can make our own wrapper to avoid this, though:
template <typename T>
class auto_ptr_facade
{
public:
auto_ptr_facade(shared_ptr<T> ptr) :
mPtr(ptr),
mAuto(ptr.get())
{}
~auto_ptr_facade()
{
// doesn't actually have ownership
mAuto.release();
}
// only expose as const, cannot be transferred
const auto_ptr<T>& get() const
{
return mAuto;
}
operator const auto_ptr<T>&() const
{
return get();
}
private:
auto_ptr_facade(const auto_ptr_facade&);
auto_ptr_facade& operator=(const auto_ptr_facade&);
shared_ptr<T> mPtr;
auto_ptr<T> mAuto;
};
Now you can treat a shared_ptr
like a const auto_ptr
, in a scope:
template <typename T>
void foo(shared_ptr<T> ptr)
{
auto_ptr_facade<T> a(ptr);
// use a
}
Usually it's a bad idea, since both auto_ptr
and shared_ptr
take the ownership of your pointer (they'll care about destroying it and stuff, according to different policies).
Having two different objects holding the ownership for the same pointer will likely result in run time errors unless your doing it for some really good (and weird!) reasons.
Assuming you want to transfer ownership from a shared_ptr
to auto_ptr
, this is only possible when
- The reference count of the
shared_ptr
is 1, and - the
shared_ptr
was originally created with a custom deleter function, and - you know the type of that deleter function.
Given that, here's how:
#include <iostream>
#include <boost/shared_ptr.hpp> // boost::shared_ptr
#include <memory> // std::auto_ptr
typedef boost::shared_ptr<int> IntSharedPtr;
typedef std::auto_ptr<int> IntAutoPtr;
template< class Type >
void myCustomDeleter( Type* p )
{
delete p;
}
IntSharedPtr newSharedInt()
{
return IntSharedPtr( new int( 42 ), &myCustomDeleter<int> );
}
IntAutoPtr makeAutoFrom( IntSharedPtr& sp )
{
struct Dummy
{
static void deleter( int* ) {}
};
typedef void (*DeleterFunc)( int* );
if( sp.use_count() > 1 ) { return IntAutoPtr( 0 ); }
DeleterFunc* d = boost::get_deleter<DeleterFunc>( sp );
if( d == 0 ) { return IntAutoPtr( 0 ); }
int* const p = sp.get();
*d = &Dummy::deleter;
sp.reset();
return IntAutoPtr( p );
}
template< class T >
T& refTo( T const& r ) { return const_cast< T& >( r ); }
int main()
{
IntAutoPtr p( makeAutoFrom( refTo( newSharedInt() ) ) );
std::cout << (p.get() == 0? "Failed" : "Worked" ) << std::endl;
}
Note: this technique isn't thread-safe.
Cheers & hth.,
You should not do that, as auto_ptr
takes ownership of the pointer.
But you can do it, but make sure you call release
before you fall out of scope.
void foo(shared_ptr<Y> s)
{
auto_ptr<Y> a(s.get());
// use a
a.release();
}
EDIT: The above solution is not exception safe. The following should work, combining a guard class with the guarantee that a const auto_ptr
cannot be copied:
void bar(const auto_ptr<Y>& p);
struct as_const_auto_ptr
{
explicit as_const_auto_ptr(const shared_ptr<Y>& p) : p(p), a(p.get()) {}
~as_const_auto_ptr() {a.release();}
operator const auto_ptr<Y>&() {return a;}
const shared_ptr<Y> p;
auto_ptr<Y> a;
};
void foo(shared_ptr<Y> s)
{
as_const_auto_ptr a(s);
// use a.
bar(a);
}
精彩评论