Attempting to compile the following code results in the following compile error:
error C2280: 'std::unique_ptr>::unique_ptr(const std::unique_ptr<_Ty,std::default_delete<_Ty>> &)' : attempting to reference a deleted function
My understanding is that the array 'm_children' should be movable since the type pointed to by unique_ptr has a move constructor defined.
Unless this is an error being caused by the recursive nature of the class or some element of move semantics that I have overlooked?
#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;
}
It seems like in vs2013 that definition of std::array
in the <array>
header doesn't include a move constructor or move assignment operator.
Under C++ rules these should be automatically generated (and so unnecessary to manually define), but msvc didn't include implicit generation of those until vs2015.
according to the error message, it doesn't call the move constructor of the unique_ptr. Instead, it calls the copy-constructor of it which is not exist. So, iterate over the unique_ptrs and call move constructor explicitly.
int i = 0;
for(auto& x : other.m_children)
m_children[i++] = std::move(x);
Btw, normally it should work because std::array is movable if and only if the element that it stores is movable too. In you case, unique_ptr is movable so that it shouldn't be problem. Please refer to below link for more detailed discussion
Is std::array movable?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With