How do you make std::shared_ptr not call delete()

Ken Li picture Ken Li · Nov 21, 2013 · Viewed 14.7k times · Source

I have functions that take in std::shared_ptr as an argument so I am forced to use std::shared_ptr, but the object I am passing to the function is not dynamically allocated. How do I wrap the object in std::shared_ptr and have std::shared_ptr not call delete on it.

Answer

ronag picture ronag · Nov 21, 2013
MyType t;
nasty_function(std::shared_ptr<MyType>(&t, [](MyType*){}));