Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove from a std::set<shared_ptr<T>> by T*

I have a set of shared pointers:

std::set<boost::shared_ptr<T>> set;

And a pointer:

T* p;

I would like to efficiently remove the element of set equal to p, but I can't do this with any of the members of set, or any of the standard algorithms, since T* is a completely different type to boost::shared_ptr<T>.

A few approaches I can think of are:

  • somehow constructing a new shared_ptr from the pointer that won't take ownership of the pointed to memory (ideal solution, but I can't see how to do this)
  • wrapping / re-implementing shared_ptr so that I can do the above
  • just doing my own binary search over the set
like image 637
James Avatar asked Dec 22 '22 04:12

James


1 Answers

Construct a shared_ptr<T> from T with a null_deleter (see boost:::shared_ptr FAQ).

struct null_deleter {
    void operator()(void const *) const { }
};

size_t remove_ptr_from_set(std::set<boost::shared_ptr<T>> &set, X* x)
{
    shared_ptr<X> px(x, null_deleter());
    return set.erase(px);
}

That way the types are compatible and you don't have to worry about your temporary shared_ptr deleting any object.

Or, as one of the comments say, if you can change T to inherit from enable_shared_from_this you could get a correct shared ptr from your object.

like image 104
Macke Avatar answered Jan 08 '23 23:01

Macke