在for循环中构造共享_ptr并移动分配
目前,我正在努力使自己的头指针以及它们如何工作。我真的很感激您可以在下面提供的任何建议。
请您建议以下建议:
何时使用std :: move in make_shared
共享指针指向使用使用make_shared在使用emplace_back和某些用户输入构建的对象上构造的共享指针,然后该对象从spope中消失,例如,for for for for loop我写了,
vector_declared_outside_for_loop.emplace_back(std::make_shared(object(std::string user_input)))
了for循环结束?
- 如果我现在从vector_declared_outside_for_loop和其他一些变量(例如loop for loop eg eg中的int number_input)制作了一个共享_ptrs的对向量。
std::vector<std::pair<shared_ptr<object>, int>> new_vector;
for(int i{0}; i<.size(); ++i){
new_vector.push_back(std::make_pair(shared_ptr<object> vector_declared_outside_for_loop [i], int connection_type));
}
new_vector中的共享_ptr是否指向任何东西?还是我只是将new_vector推回?
I'm trying to get my head around shared pointers at the moment and how they work. I would be really grateful for any advice you could give on the below.
Please could you advise on:
When to use std::move in make_shared
What the shared pointer points to if it is constructed using make_shared on an object which is constructed using emplace_back and some user input and that object then disappears from scope e.g. in a for loop I write,
vector_declared_outside_for_loop.emplace_back(std::make_shared(object(std::string user_input)))
, what happens to the shared_ptr in the for loop after the for loop ends?
- If I now make a vector of pairs of the shared_ptrs from vector_declared_outside_for_loop and some other variable such as int number_input in a subsequent for loop eg.
std::vector<std::pair<shared_ptr<object>, int>> new_vector;
for(int i{0}; i<.size(); ++i){
new_vector.push_back(std::make_pair(shared_ptr<object> vector_declared_outside_for_loop [i], int connection_type));
}
Do the shared_ptrs in new_vector point to anything? Or am I just pushing new_vector back?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用
std :: Move
每当您拥有不再使用的命名对象时。只需忘记所有您认为您对std :: Move
的了解,然后学习一个简单的规则:std ::移动
表示:我不再需要此(名称对象) 。因此,
func1(std :: move(func2(...)))
毫无意义。返回的对象没有名称,您无法再次使用它。编译器知道这一点,并且将在没有std :: Move
的情况下使用移动语义。说我不再需要这个是毫无意义的,因为func1
调用临时对象不再存在。make_shared分配了一个新对象,并将其构造到位。但是,当您
emplace_back
您要安置的是共享_ptr,而不是它指向的对象。因此,发生的事情是shared_ptr
是在vector
内构建的,并用指向对象和构造对象的指针。shared_ptr
完全不存在。这就是emplace_back
它不会创建临时对象,而是直接在向量内部构造对象。push_back
通常将对象带入向量。因此,您将创建一个临时对象,复制并破坏临时对象。但是STL并不愚蠢,也涵盖了这种情况。如果您push_back
可以移动的对象,则将其转发到emplace_back
。因此,这对再次将直接在向量内部构造。这留下了
vector_declared_outside_for_for_loop
。由于它是现有对象,因此无法在就地构造。由于它是命名对象,因此无法移动。因此,将复制对象。对于
shared_ptr
,这意味着将复制内部指针,并且对象的参考计数将在原子上递增。因此,在循环结束时,shared_ptr
将具有.size() + 1
的参考计数。然后,在范围的末尾,原始shared_ptr
不范围将参考计数降低1。向量中使用的对象保留。You use
std::move
whenever you have a named object that you no longer use. Just forget all you think you know aboutstd::move
and learn one simple rule:std::move
means: I no longer need this (name object).So
func1(std::move(func2(...)))
makes no sense. The returned object has no name, there is no way for you to use it again. The compiler knows that and will already use move semantic withoutstd::move
. Saying I no longer need this is pointless because after thefunc1
call the temporary object no longer exists.make_shared allocates a new object to point to and constructs it in place. But then when you
emplace_back
what you are emplacing is the shared_ptr, not the object it points to. So what happens is thatshared_ptr
is constructed in place inside thevector
with a pointer to the object it alloctes and constructs.The
shared_ptr
in the for loop doesn't exist at all. That's the point of theemplace_back
that it doesn't create a temporary object but constructs the object in-place inside the vector directly.push_back
normally takes an object and copies it into the vector. So you would create a temporary object, copy it and destroy the temporary. But the STL isn't stupid and covers this case too. If youpush_back
an object that can be moved then it is forwarded toemplace_back
. So again the pair will be constructed directly in-place inside the vector.That leaves the
vector_declared_outside_for_loop
. Since it is an existing object it can't be constructed in-place. Since it is a named object it can not be moved. So the objects will be copied.For a
shared_ptr
that means the internal pointer will be copied and the reference count for the object will be incremented atomically. So at the end of the loop theshared_ptr
will have a reference count of.size() + 1
. Then at the end of the scope the originalshared_ptr
goes out of scope reducing the reference count by 1. The object used in the vector remains.