Question

So I'm working through a book about game development with SFML and C++11, and one of the lines of code used to create a scene tree is giving me trouble that is a little over my head at this point. The compiler is returning an error due to an implicitly deleted copy constructor of the unique_ptr in the find_if algorithm.

Here's the function with the find_if call. Ptr is a typedef for a std::unique_ptr<SceneNode>. This is the only place where I use find_if currently.

SceneNode::Ptr SceneNode::detachChild(const SceneNode& node) {
    auto found = std::find_if(mChildren.begin(), mChildren.end(), [&] (Ptr p) -> bool { return p.get() == &node; });

    assert(found != mChildren.end());

    Ptr result = std::move(*found);
    result->mParent = nullptr;
    mChildren.erase(found);
    return result;
}

The error returned is brought up within the algorithm itself, and is specifically "call to implicitly-deleted copy constructor of 'Ptr'."

There is a related question located at Call to implicitly deleted copy constructor in LLVM, but the answer isn't making much sense in my case.

As a note, I'm using the latest Xcode 5 release for development.

Was it helpful?

Solution

The lambda expression in the find_if call takes a Ptr (aka unique_ptr<SceneNode>) argument by value, which means it's attempting to copy the unique_ptr; unique_ptrs are non copyable, hence the error.

Change the lambda expression to the following:

[&] (Ptr const& p) -> bool { return p.get() == &node; }
//       ^^^^^^
// take the argument by reference and avoid copying
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top