std::unique_ptr<T> incomplete type error
c++, c++11, templates, unique-ptr, visual-studio-2010
Solution
The requirement of complete type at the template instantiation point is due to `std::default_delete`, so probably you could work-around it by providing a custom deleter.
struct node; // node_delete need to know 'node' is a type.
struct node_deleter
{ // std::unique_ptr needs 'node_deleter' to be complete.
void operator()(node* ptr); // forward-reference to avoid needing
}; // 'delete ptr' before the node is complete.
struct node
{
std::unique_ptr<node, node_deleter> next;
};
void node_deleter::operator()(node* ptr)
{
delete ptr;
}
Mind you, I have not tested it in MSVC, and probably you should try to upgrade to VC 11, or file a bug to Microsoft if even VC 11 cannot compile your original code.
Problem
I have ``` template<typename T> class queue { private: struct node { T data; std::unique_ptr<node> next; //compile error on incomplete type node(T&& data_): data(std::move(data_)) {} }; std::unique_ptr<node> head; node* tail; public: queue(): tail(nullptr) {} ``` I get compile error on marked line in VS10. Shouldn't I be allowed to use an incomplete type in this case (instantiate the template - the constructor - here for int as an example) ? Is there a work-around ? EDIT ``` singlethreadedqueue.h(62): error C2079: 'queue<T>::node::next' uses undefined class 'std::unique_ptr<_Ty>' 1> with 1> [ 1> T=MyClass 1> ] 1> and 1> [ 1> _Ty=queue<MyClass>::node 1> ] 1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2161) : see reference to class template instantiation 'queue<T>::node' being compiled 1> with 1> [ 1> T=MyClass 1> ] 1> c:\program files\microsoft visual studio 10.0\vc\include\memory(2195) : see reference to class template instantiation 'std::_Unique_ptr_base<_Ty,_Dx,_Empty_deleter>' being compiled 1> with 1> [ 1> _Ty=queue<MyClass>::node, 1> _Dx=std::default_delete<queue<MyClass>::node>, 1> _Empty_deleter=true 1> ] 1> singlethreadedqueue.h(69) : see reference to class template instantiation 'std::unique_ptr<_Ty>' being compiled 1> with 1> [ 1> _Ty=queue<MyClass>::node 1> ] 1> : see reference to class template instantiation 'queue<T>' being compiled 1> with 1> [ 1> T=MyClass 1> ] ```