Sometimes I need shared_ptr instances that have a no-op deleter, because an API expects a shared_ptr instance that it wants to store for a limited time but I am given a raw pointer that I am not allowed to own for a time larger than what I am running for.
For this case, I have been using a no-op deleter, such as [](const void *){}, but today I found that there's another alternative to that, using (or abusing?) the aliasing constructor of shared_ptr:
void f(ExpectedClass *ec) {
std::shared_ptr<ExpectedClass> p(std::shared_ptr<void>(), ec);
assert(p.empty() && p.get() != nullptr);
apiCall(p);
}
My question is, what is the better way to do this and why? Are the performance expectations the same? With a no-op deleter I expect to pay some cost for the storage of the deleter and reference count, which doesn't appear to be the case when using the aliasing constructor with the empty shared_ptr.
Aucun commentaire:
Enregistrer un commentaire