在for循环中构造共享_ptr并移动分配

发布于 2025-01-29 13:44:51 字数 838 浏览 2 评论 0原文

目前,我正在努力使自己的头指针以及它们如何工作。我真的很感激您可以在下面提供的任何建议。

请您建议以下建议:

  • 何时使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-02-05 13:44:51
  • 您使用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 about std::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 without std::move. Saying I no longer need this is pointless because after the func1 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 that shared_ptr is constructed in place inside the vector 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 the emplace_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 you push_back an object that can be moved then it is forwarded to emplace_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 the shared_ptr will have a reference count of .size() + 1. Then at the end of the scope the original shared_ptr goes out of scope reducing the reference count by 1. The object used in the vector remains.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文