std :: unique_ptr< T>类型不完整错误 [英] std::unique_ptr<T> incomplete type error

查看:83
本文介绍了std :: unique_ptr< T>类型不完整错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

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)
        {}

我在VS10中的标记行上出现编译错误。在这种情况下,是否应该允许我使用不完整的类型(实例化模板-构造函数-这里以int为例)?有解决方法吗?

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 ?

编辑

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>          ]


推荐答案

在模板实例化点的完整类型要求是由于 std :: default_delete ,所以您可以通过提供自定义删除器来解决它。

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;
}

请记住,我尚未在MSVC中对其进行测试,也许您应该尝试升级到VC 11,或者即使VC 11也不能编译您的原始代码,也向Microsoft提交错误

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.

这篇关于std :: unique_ptr&lt; T&gt;类型不完整错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆