因此,我正在阅读一本关于使用 SFML 和 C++11 进行游戏开发的书,其中用于创建场景树的代码行给我带来了麻烦,此时我有点难以理解。由于 find_if 算法中隐式删除了 unique_ptr 的复制构造函数,编译器返回错误。
这是调用 find_if 的函数。 Ptr 是 std::unique_ptr<SceneNode>
的类型定义.这是我目前唯一使用 find_if 的地方。
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;
}
返回的错误是在算法本身中产生的,具体是“调用‘Ptr’的隐式删除的复制构造函数。”
有一个相关问题位于 Call to implicitly deleted copy constructor in LLVM ,但答案对我来说意义不大。
请注意,我正在使用最新的 Xcode 5 版本进行开发。
最佳答案
find_if
中的 lambda 表达式调用需要 Ptr
(又名 unique_ptr<SceneNode>
)参数 value,这意味着它试图复制 unique_ptr
; unique_ptr
s 不可复制,因此会出现错误。
将 lambda 表达式更改为以下内容:
[&] (Ptr const& p) -> bool { return p.get() == &node; }
// ^^^^^^
// take the argument by reference and avoid copying
关于c++ - LLVM find_if 使用 unique_ptr<> 隐式删除复制构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23071621/