1

I am currently in the process of learning smart pointers and try to avoid using raw pointers.

I have a vector with shared ptrs

std::vector<std::shared_ptr<View>> mChildren;

and an Add and Remove method

void View::AddChild(std::shared_ptr<View> view) {
    mChildren.push_back(view);
}

void View::RemoveChild(std::shared_ptr<View> view) {
    auto removal = std::remove(mChildren.begin(), mChildren.end(), view);
    mChildren.erase(removal, mChildren.end());
}

Now in another part of my code I have a map

std::map<std::weak_ptr<ModelGem>,std::unique_ptr<ViewGem>,std::owner_less<std::weak_ptr<ModelGem>>> mViews;

Now when I try to remove elements from the map like this:

for (auto iterator = mViews.begin(); iterator != mViews.end();) 
{
    if (iterator->first.expired()) 
    {
        RemoveChild(iterator->second.get());
        iterator = mViews.erase(iterator);
    }
    else 
    {
        iterator++;
    }
}

Now the problem lies here : iterator->second.get() It tells me it cannot convert the rvalue of type pointer to shared ptr. However if I use raw pointers instead of shared pointers this is not an issue at at all.

So, I am wondering if in this case it would be better to just use raw pointers or can I work around this with shared pointers?

4

1 に答える 1