Moving the unique_ptr <T> array to a recursive data structure

Attempting to compile the following code results in the following compilation error:

error C2280: "Stand :: unique_ptr> :: unique_ptr (Const std :: unique_ptr <_Ty, std :: default_delete <_Ty → ​​&) ': attempt to reference a remote function

I understand that the array "m_children" must be movable, because the type pointed to by unique_ptr has a move constructor.

If this is an error caused by the recursive nature of the class or some element of the displacement semantics, which I missed?

#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;
}
+4
source share
3 answers

, vs2013 std::array <array> .

++ ( ), msvc , vs2015.

+6

, unique_ptr. -, . , unique_ptrs move .

    int i = 0;
    for(auto& x : other.m_children)
        m_children[i++] = std::move(x);

Btw, , , std:: array , , , . unique_ptr , . .

std:: array ?

+2

I do not believe that std :: array would have compiler generated operations if there are copies, or am I mistaken?

And as a side element to moving these unique_ptrs, you can use the std :: move algorithm instead of the raw loop:

std::move(std::begin(other.m_children), std::end(other.m_children), std::begin(m_children));
0
source

Source: https://habr.com/ru/post/1598428/


All Articles