简体   繁体   English

unique_ptr(std :: nullptr_t)和模板

[英]unique_ptr(std::nullptr_t) and templates

I'm making a component-entity system for a game. 我正在为游戏制作一个组件实体系统。

I have a class, Object, which is just a shell to hold components, which in turn are stored in the object in a std::map as a (typeid, unique_ptr<>) pair. 我有一个Object类,它只是一个用于容纳组件的外壳,这些组件又作为一对(typeid,unique_ptr <>)存储在对象的std :: map中。 Each component is a class derived from Component. 每个组件都是派生自Component的类。

The Object class has several template functions (the class itself is not a template though) to facilitate access and modification to the components. Object类具有多个模板函数(尽管该类本身不是模板),以方便对组件的访问和修改。 The template definitions are in the header file with the class definition (to avoid linker errors). 模板定义位于带有类定义的头文件中(以避免链接器错误)。

When compiling, I receive the following error (in VS2012): 编译时,出现以下错误(在VS2012中):

error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(std::nullptr_t) throw()' : cannot convert parameter 1 from 'std::unique_ptr<_Ty>' to 'std::nullptr_t'
      with
      [
          _Ty=Positional,
          _Dx=std::default_delete<Positional>
      ]
      and
      [
          _Ty=Component
      ]
      nullptr can only be converted to pointer or handle types
see reference to function template instantiation 'std::unique_ptr<_Ty,_Dx> Object::__GetComponent<Positional>(void)' being compiled
          with
          [
              _Ty=Positional,
              _Dx=std::default_delete<Positional>
          ]

in reference to the following function: 参考以下功能:

template <typename T> std::unique_ptr<T>
Object::__GetComponent()
{
    if(m_components.count(&typeid(T)) != 0)
    {
        return m_components[&typeid(T)];
    }
    else 
    {
        return std::unique_ptr<T>(new T);
    }
}

What is going on here? 这里发生了什么? No where in my code do I use a custom deleter, nor do I try to store a nullptr in a unique_ptr instance. 在我的代码中,我没有地方使用自定义删除器,也没有尝试将nullptr存储在unique_ptr实例中。 I guess more specifically, what is trying to cause the conversion from unique_ptr<> to nullptr_t? 我想更具体地说,是什么导致了从unique_ptr <>到nullptr_t的转换?

I also receive this error for every component, not just the 'Positional' one listed. 我还会收到每个组件的错误,而不仅仅是列出的“位置”组件。

Here: 这里:

return m_components[&typeid(T)];

you're trying to return a copy of one of the unique pointers in the map. 您正在尝试返回地图中唯一指针之一的副本。 Unique pointers can't be copied; 唯一的指针不能被复制; only one can own any given object, hence the "unique" part of the name. 只有一个人可以拥有任何给定的对象,因此是名称的“唯一”部分。

The error message is rather cryptic; 该错误消息相当隐晦; a more sensible compiler would complain that the copy constructor is deleted, which should make it clear what's wrong. 一个更明智的编译器会抱怨复制构造函数被删除,这应该清楚出什么问题。 This is apparently ignoring the copy constructor altogether, and only mentioning a different, unsuitable constructor. 显然,这完全忽略了复制构造函数,仅提及了另一个不合适的构造函数。

It's not entirely clear what you actually want to return, and how the ownership of the objects should be managed. 尚不清楚您实际上想要返回什么,以及如何管理对象的所有权。 You'll need to decide on the strategy for managing the objects, and use smart pointers appropriately. 您需要确定管理对象的策略,并适当使用智能指针。 Perhaps you want to remove the element from the map and return its unique pointer (by moving it). 也许您想从地图上删除该元素并返回其唯一指针(通过移动它)。 Perhaps you want to make a new copy of the object and return a unique pointer managing that. 也许您想创建该对象的新副本并返回一个唯一的指针来管理该对象。 Perhaps you want to return a non-owning pointer; 也许您想返回一个非所有者指针; but then it's not clear what to do if it's not in the map (add a new element, maybe?). 但是,如果它不在地图中,那么就不清楚该怎么做(也许添加一个新元素?)。 Perhaps you want to store and return shared pointers, so that they can remain in the map but share ownership with whoever calls this function. 也许您想存储和返回共享指针,以便它们可以保留在地图中,但与调用此函数的人共享所有权。 Perhaps you want to do something entirely different. 也许您想做一些完全不同的事情。

Also, you shouldn't call the function __GetComponent . 另外,您不应调用__GetComponent函数。 That's a reserved name . 那是保留名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM