A common index can be shared by many things, you cannot just take it from them somehow. It is described by Artyom and peoro .
One approach is to create a temporary auto_ptr and free it from processing the pointer at the end of the scope. dalle describes the first approach, but it suffers from an insecurity of exceptions (it may accidentally be deleted), and it cannot protect you from accidentally passing it to a function that will (where the deletion falls out of our hands).
We can make our own wrapper to avoid this:
template <typename T> class auto_ptr_facade { public: auto_ptr_facade(shared_ptr<T> ptr) : mPtr(ptr), mAuto(ptr.get()) {} ~auto_ptr_facade() {
Now you can consider shared_ptr as a const auto_ptr in scope:
template <typename T> void foo(shared_ptr<T> ptr) { auto_ptr_facade<T> a(ptr);
GManNickG
source share