C++ 如何为 std::vector 创建 shared_ptr?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26734452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:40:49  来源:igfitidea点击:

How can I create a shared_ptr to a std::vector?

c++c++11

提问by Oscar_Mariani

I need to create a shared_ptr to a std::vector, what is the correct syntax?

我需要为 std::vector 创建一个 shared_ptr,正确的语法是什么?

std::vector<uint8_t> mVector;
shared_ptr<std::vector<uint8_t>> mSharedPtr = &mVector;

The code above does not compile.

上面的代码不能编译。

Thanks.

谢谢。

回答by davidhigh

What you are trying to do is to let a smart pointer manage a stack object. This doesn't work, as the stack object is going to kill itself when it goes out of scope. The smart pointer can't prevent it from doing this.

您要做的是让智能指针管理堆栈对象。这不起作用,因为堆栈对象在超出范围时会自行终止。智能指针无法阻止它这样做。

std::shared_ptr<std::vector<uint8_t> > sp;
{
   std::vector<uint8_t> mVector;
   sp=std::shared_ptr<std::vector<uint8_t> >(&mVector);
}
sp->empty();   // dangling reference, as mVector is already destroyed


Three alternatives:

三种选择:

(1) Initialize the vector and let it manage by the shared_ptr:

(1) 初始化向量并让它由以下管理shared_ptr

auto mSharedPtr = std::make_shared<std::vector<uint8_t> >(/* vector constructor arguments*/);



(2) Manage a copy of the vector (by invoking the vector copy constructor):

(2) 管理vector的副本(通过调用vector copy构造函数):

std::vector<uint8_t> mVector;
auto mSharedPtr = std::make_shared<std::vector<uint8_t> >(mVector);



(3) Move the vector (by invoking the vector move constructor):

(3) 移动向量(通过调用向量移动构造函数):

std::vector<uint8_t> mVector;
auto mSharedPtr = std::make_shared<std::vector<uint8_t> >(std::move(mVector));
//don't use mVector anymore.


回答by coyotte508

First, what you're doing is very wrong, if you give a pointer to a shared_ptr make sure it's dynamically allocated with new, not on the stack. Otherwise you may just as well use a pointer instead of a shared_ptr.

首先,你在做什么是非常错误的,如果你给一个 shared_ptr 一个指针,确保它是动态分配的new,而不是在堆栈上。否则,您也可以使用指针而不是 shared_ptr。

Your code should be:

你的代码应该是:

std::vector<uint8_t> mVector;
/* Copy the vector in a shared pointer */
std::shared_ptr<std::vector<uint8_t> > mSharedPtr ( new std::vector<uint8_t>(mVector) );

or:

或者:

std::shared_ptr<std::vector<uint8_t> > mSharedPtr ( new std::vector<uint8_t>() );

As for why it doesn't compile, you need to use the constructor, not the =operator.

至于为什么不编译,你需要使用构造函数,而不是=运算符。

As pointed out by @remyabel, make_sharedis more efficient:

正如@remyabel 所指出的,make_shared更有效:

std::vector<uint8_t> vector;
/* Copy the vector in a shared pointer */
auto sharedPtr = std::make_shared<std::vector<uint8_t>> (vector);

回答by M. Khalil

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.

您的代码无法编译,因为您将原始指针 ' &mVector'分配给智能指针 ' mSharedPtr',这是两个不同的对象,并且不允许进行强制转换。

you can do that by other approaches

你可以通过其他方法做到这一点

(1) intializing your shared_ptrwith the raw pointer from the begining

(1)shared_ptr从一开始就用原始指针初始化你

std::shared_ptr<std::vector<uint8_t>> sPtr (&mVector);

(2) using reset()method of shared_ptr

(2)使用reset()方法shared_ptr

std::shared_ptr<std::vector<uint8_t>> 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<std::vector<uint8_t>> sPtr (&mVector,[](std::vector<uint8_t>*){});