从unordered_set有效地擦除unique_ptr [英] Efficiently erase a unique_ptr from an unordered_set
问题描述
我正在使用 unique_ptr
s将某些对象的所有权存储在 unordered_set
内。
但是我不知道有什么好方法可以在时间到时从集合中删除其中的一个。
I am storing the ownership of some objects inside an unordered_set
, using unique_ptr
s.
But I don't know a good way to erase one of them from the set, when the time comes.
代码如下所示:
typedef unique_ptr<MyType> MyPtr;
unordered_set<MyPtr> owner;
MyPtr p = make_unique<MyType>("foo")
MyType *pRaw = p.get();
owner.insert(std::move(p));
// Later ...
// I want to do something like this (cannot be written as-is, of course):
// owner.erase(pRaw);
有没有办法做到这一点?
当然,我可以使用 begin()
和 end()
迭代整个集合,但是
Is there a way to do this?
I can, of course, iterate the entire set with begin()
and end()
, but the whole point of putting them in the set is to make these lookups efficient.
我已经想到的一些事情是:
Some things I have thought of already:
- 使用
shared_ptr
。这是我的情况的错误抽象。所有权是唯一的。 - 使用原始指针,而不必考虑unique_ptr。这就放弃了
unique_ptr
提供的所有优势。 - 使用
unordered_set :: begin(key)查找存储桶
。据我所知,我无法创建与要删除的unique_ptr
匹配的密钥。但是我很高兴被证明是错误的(:
- Use
shared_ptr
. This is the wrong abstraction for my case. Ownership is unique. - Use raw pointers, and forget about unique_ptr. This abandons all the advantages that
unique_ptr
provides. - Find the bucket with
unordered_set::begin(key)
. As far as I know, there is no way for me to create a key that will match theunique_ptr
I want to delete. But I'm happy to be proven wrong (:
(实际上,我使用 eastl解决了这个问题: :unordered_set
,其 find_as
用于自定义键的功能)
(In truth, I solved this using eastl::unordered_set
, with its find_as
function for custom keys)
推荐答案
这是擦除
有一个重载,该重载带有 const key_type&
参数,因此我们可以尝试创建一个过期 unique_ptr
来获取要删除的元素的哈希值:
This is a tough case. erase
has an overload that takes a const key_type&
parameter, so we can try to create a "stale" unique_ptr
to get the hash value of the element to be erased:
template <typename T>
auto erase(std::unordered_set<std::unique_ptr<T>>& set, T* ptr)
{
std::unique_ptr<T> stale_ptr{ptr};
auto ret = set.erase(stale_ptr);
stale_ptr.release();
return ret;
}
(实时演示)
但是,此版本在一般,因为如果 set.erase
引发异常,则不会调用 release
。在这种情况下这不是问题,因为 std :: equal_to< std :: unique_ptr< T>> :: operator()
永远不会引发异常。在一般情况下,我们可以通过确保调用 release
来滥用 unique_ptr
(!)来强制异常安全。该函数是正常还是异常退出:
This version, however, is not exception safe in general, because release
will not be called if set.erase
throws an exception. This is not a problem in this case, since std::equal_to<std::unique_ptr<T>>::operator()
never throws exception. In the general case, we can abuse unique_ptr
(!) to enforce exception safety by ensuring that release
is called regardless of whether the function is exited normally or exceptionally:
template <typename T>
auto erase(std::unordered_set<std::unique_ptr<T>>& set, T* ptr)
{
std::unique_ptr<T> stale_ptr{ptr};
auto release = [](std::unique_ptr<T>* p) { p->release(); };
std::unique_ptr<std::unique_ptr<T>, decltype(release)> release_helper{&stale_ptr, release};
return set.erase(stale_ptr);
}
(实时演示)
这篇关于从unordered_set有效地擦除unique_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!