Search code examples
c++c++17shared-ptr

How to get the raw pointer of a shared_ptr in C++17?


I'm working with a C library and would like to covert an object pointer to a shared_ptr. Say the C library has...

T* CreateObject();
void DoStuff(T*);
void DestroyObject(T*);

Then I can do...

std::shared_ptr<T> sptr(CreateObject(), DestroyObject);

While I can put the pointer from the CreateObject method into a shared_ptr, and call the DestroyObject method with a custom deleter, there is the issue of accessing the raw pointer to call DoStuff. I noticed the shared_ptr::get() method but it was removed in C++17 as far as I can tell.


Solution

  • shared_ptr::get() still exists in C++17.

    Its definition was merely refined.

    Before C++17, shared_ptr<T>::get() returned T*. Since C++17, it returns std::remove_extent_t<T>*.

    This change was made to mirror boost::shared_ptr's handling of arrays.

    You may still use it.