jeudi 30 avril 2020

How should I pass a unique_ptr through 2 function calls with move semantics?

Simply, in some part of code, I create an object and assign to unique_ptr:

unique_ptr<Node> n ( CreateNode() );
container.place(id, std::move(n));

The definition of place is:

void place(int id, unique_ptr<Node> n)
{
    mappings_[id] = std::move(n);  // std::map<int, unique_ptr<Node>> mappings_;
}

I'm currently trying to hunt a bug where the alleged object that was retrieved from the map has some internal pointers completely twisted, which might have been a consequence of that the object was really deleted, but this fact was somewhat missed. Questions:

  1. Should this n be of unique_ptr<Node> type or unique_ptr<Node>&&?
  2. What are the consequences of both of the above?

Aucun commentaire:

Enregistrer un commentaire