I need to create a shared_ptr to a std::vector, what is the correct syntax?
std::vector mVector;
shared_ptr> mSha
your code doesn't compile because you are assigning a raw pointer '&mVector
' to smart pointer 'mSharedPtr
' which are two different objects and no casting is allowed.
you can do that by other approaches
(1) intializing your shared_ptr
with the raw pointer from the begining
std::shared_ptr> sPtr (&mVector);
(2) using reset()
method of shared_ptr
std::shared_ptr> sPtr;
sPtr.reset(&mVector);
assigning a stack object raw pointer to smart pointer , you should also supply an empty deleter to the smart pointer, so that the smart pointer doesn't delete the object when it is still on the stack.
std::shared_ptr> sPtr (&mVector,[](std::vector*){});