c++ - LLVM find_if 使用 unique_ptr<> 隐式删除复制构造函数

标签 c++ xcode c++11 llvm unique-ptr

因此,我正在阅读一本关于使用 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/

相关文章:

iphone - 如何将 C++ 库链接到 Objective-C 程序

C++ 模板返回带有类型名值的元组

ios - 如何在模拟器中更改高速公路行驶的轨道?

ios - Apple Pay 不适用于发布版本(适用于调试)

c++ - std::lock_guard 有什么问题

c++ - 来自多行文件 C++ 的 vector 矩阵

c++ - 切换枚举时在 switch 语句中使用默认值

c++ - 全局运算符和成员运算符的区别

ios - Xcode 分析工具调用树无法识别我的源代码

c++ - 空基类构建开销?