gpt4 book ai didi

c++ - 在递归数据结构中 move unique_ptr 数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:22:47 25 4
gpt4 key购买 nike

尝试编译以下代码会导致以下编译错误:

error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function

我的理解是数组“m_children”应该是可 move 的,因为 unique_ptr 指向的类型定义了 move 构造函数。

除非这是由类的递归性质或我忽略的某些 move 语义元素引起的错误?

#include <array>
#include <memory>
#include <iostream>

class OctreeNode{
public:
OctreeNode(){ };
OctreeNode(OctreeNode&& other) : m_children(std::move(other.m_children)){};
private:
std::array<std::unique_ptr<OctreeNode>, 8> m_children;
};

int main(int argc, char* argv[])
{
OctreeNode T;
std::cout << "Success!" << std::endl;
return 0;
}

最佳答案

似乎在 vs2013 中 std::array 的定义在<array> header 不包含 move 构造函数或 move 赋值运算符。

根据 C++ 规则,这些应该自动生成(因此无需手动定义),但 msvc 直到 vs2015 才包含这些隐式生成。

关于c++ - 在递归数据结构中 move unique_ptr<T> 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31482317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com