c++ - How to wrap a raw pointer into a shared_ptr and prevent shared_ptr from deleting the object? -
I need to wrap a raw pointer in a How to prevent As stated in the comments, type an empty deleter: Examples of use: output: living alive Of course, this example is leaked memory. shared_ptr to pass the function. The function does not return once in any reference to the input object.
{MyClass i; Shared_ptr & lt; MyClass & gt; P (& amp; i); F (p); / / BAD: shared_ptr will erase I}
shared_ptr by removing the referenced object?
< Code> #include & lt; Type_traits & gt; Template & lt; Typename T & gt; Struct empty_delete {empty_delete () / * noexcept * / {} template & lt; Typename u & gt; Empty_dellete (const_delete
#include & lt; Iostream & gt; #include & lt; Memory & gt; Structure noise (noise () (std :: cout & lt; "living"
living alive
living
taking ... dead =
taking ... dead
caught Got it.
Taking ...
Detected.
Comments
Post a Comment