Creating an object and giving ownership to a container using a unique_ptr is no problem. How would one remove an element by raw pointer?
std::set<std::unique_ptr<MyClass>> mySet;
MyClass *myClass = new MyClass();
mySet.insert(std::unique_ptr<MyClass>(myClass));
// remove myClass from mySet?
You will need to find the iterator corresponding to the
myClasselement and then pass that iterator tomySet.erase(). The iterator may be found using thestd::find_ifalgorithm with a customPredicatefunctor that understands how to dereferenceunique_ptrand compare it to the raw pointermyClass.You can not use the overloaded
size_t set::erase ( const key_type& x );since the raw pointer (even if wrapped in a temporaryunique_ptr) will not be found inmySet.