I have a container of shared_ptrs and I hand these objects off to a windows API and I get a callback later with the raw ptr. I want to locate the right shared_ptr after the fact. Can this be done with shared_ptr cleanly? (without using shared_from_this()).
very basic example:
class CFoo
{
};
typedef std::shared_ptr<CFoo> CFooPtr;
typedef std::set<CFooPtr> CFooSet;
extern CFooSet m_gSet;
void SomeWindowsCallBack(CFoo* pRawPtr)
{
m_gSet.erase(pRawPtr);
}
I know that this can be done with intrusive_ptr very easily but I am curious if there is a way with shared_ptr. Aka I am looking for the container to accept the RawPtr and the shared_ptr for locating the shared_ptr item. The issue is that I can’t implicitly cast the CFoo* into the shared_ptr (for reasons I do understand).
I was thinking I could do
m_gSet.erase(shared_ptr<CFoo>(pRawPtr, _do_not_delete_deleter))
but I have not tried that yet and it seems dangerous/ugly. Is there another way or am I basically looking for intrusive_ptr? Thanks
Why not the obvious way? Iterate through the container, and
Edit: To utilize O(logN) lookup you can do what you want (that is, create a shared_ptr with no_op deleter). It may be ugly, but it’s not dangerous