Assume I have a set of unique_ptr:
std::unordered_set > my_set;
I\'m not sure what\'s the safe way to
You can also use a deleter that optionally doesn't do anything.
template
struct maybe_deleter{
bool _delete;
explicit maybe_deleter(bool doit = true) : _delete(doit){}
void operator()(T* p) const{
if(_delete) delete p;
}
};
template
using set_unique_ptr = std::unique_ptr>;
template
set_unique_ptr make_find_ptr(T* raw){
return set_unique_ptr(raw, maybe_deleter(false));
}
// ...
int* raw = new int(42);
std::unordered_set> myset;
myset.insert(set_unique_ptr(raw));
auto it = myset.find(make_find_ptr(raw));
Live example.