vendredi 30 janvier 2015

Moving a smart pointer from one unordered_set from another


template<class T>
Class Node
{
//irrelavant functs
};
class A
{
unordered_set<unique_ptr<Node<T>>, myHash<Node<T>>, myEqual<Node<T>>> nodes
shared_ptr<A> child;

void moveToChild()
{
for(auto it = nodes.begin(); it < nodes.end(); ++it) {
if (some_cond) {
child->nodes.emplace(std::move(*it));
}
}
}
};


I have a class that holds bunch of nodes in unordered_set, and has a pointer to itself called child. When some arbitrary conditions satisfied, this class should move some (or all) of its pointers to Node objects to child's nodes container. But I'm not sure if it's possible since keys in unordered_sets are const.


I don't mind constructing a new smart pointer, but I can't afford to construct a new node every time I move it around or remove it from unordered_set. If what I'm trying to do is not possible with unique_ptrs, I was wondering if it's possible with shared_ptrs?


I have never implemented my own allocators for STL containers so not sure if I'm on the right track but I was thinking of writing a custom allocator for unordered_set that takes a bool, if it's true, it deletes the objet if it's false it doesn’t free the pointer but removes it from the container (again, not sure if I can make such a dramatic change to container's behavior)


So, is it possible to somehow move a smart pointer from one unordered_set to another without freeing it?


Note: Please don't pay attention to typos and syntax errors, it's a grossly simplified version of my code.


Aucun commentaire:

Enregistrer un commentaire